XF 2.0 How to handle extending the same class in different add ons?

Tipmoose

Member
I am trying to extend the XF\Repository\SessionActivity class in two different add ons. The class extension in the first add on modifies methods a and b. I am trying to create a second extension to modify method c.

XF is throwing an exception telling me i can't declare the second extension because its already been declared in the first one. Am I going to have to move my extension into some sort of shared folder and only have one? How can people have multiple add ons from different authors that extend the same class?
 
Not knowing exactly how your doing it, I'd say double check your namespace. You can't have two extensions named the same thing in the same namespace.

Also be sure you're extending like this...
Code:
class SessionActivity extends XFCP_SessionActivity
 
Not knowing exactly how your doing it, I'd say double check your namespace. You can't have two extensions named the same thing in the same namespace.

Also be sure you're extending like this...
Code:
class SessionActivity extends XFCP_SessionActivity

Yep. That was the problem. Copy/Paste error made the namespaces the same. Thanks for the help!
 
Also be sure you're extending like this...
I know it says in the man (https://xenforo.com/xf2-docs/dev/general-concepts/#extending-classes) to use the XFCP, but I don't exactly get the point of the XFCP. In extension_hint.php (generated into _output folder of your addon) it will add for
PHP:
namespace My\XF\Pub\Controller
{
   class MyForum extends XFCP_Forum {}
}
something like this:
PHP:
namespace My\XF\Pub\Controller
{
   class XFCP_Forum extends \XF\Pub\Controller\Forum {}
}
If I understand this correctly, this means MyForum extends XFCP_Forum extends XF Forum Controller.
So if there was MyForumAnother controller which would also extend the forum controller, it would go like
MyForumAnother extends XFCP_Forum extends XF Forum Controller.
What exactly is now the difference between this and directly extending the forum controller, e.g.
MyForum extends XF Forum Controller
MyForumAnother extends XF Forum Controller

I know that XFCP_Forum does not exist, but still it's getting extended twice, so some kind of magic has to be happening here?
 
I'm probably missing something really stupid, but XFCP is giving me some kind of problems. What I want to do is to use the forum controller within another controller.
I've setup a class extension (ACP) so the forum controller gets extended correctly.
But this only works by directly extending the controller:
Test\Test\XF\Pub\Controller\Forum.php
PHP:
namespace Test\Test\XF\Pub\Controller;

class Forum extends \XF\Pub\Controller\Forum {
    public function test() {
        ...
    }
}
Test\Test\Pub\Controller\Test.php
PHP:
<?php
namespace Test\Test\Pub\Controller;
use Test\Test\XF\Pub\Controller\Forum as ForumController;

class TestController extends \XF\Pub\Controller\AbstractController {
    public function actionIndex(ParameterBag $params) {
        ...
            ForumController->test();
        ...
    }
}

Using
PHP:
class Forum extends XFCP_Forum
gives me everytime
Fatal error: Class 'Test\Test\XF\Pub\Controller\XFCP_Forum' not found
 
Should be..
Code:
<?php
namespace Test\Test\XF\Pub\Controller;

class Forum extends XFCP_Forum
{
    public function test() {
        ...
    }
}

What does your directory structure look like?

Your extension should be here..

src
...addons
......Test
.........Test
............XF
...............Pub
..................Controller
.....................Forum.php
 
Well, sorry to say that if your add-on is in the src/addons directory like I showed I'm out of ideas, except for double checking capitalization, spelling, the location of the file and the name of your add-on (Test/Test).
 
You can't direcly instantiate an extension class.

PHP:
$class = $this->app()->extendClass('XF\Pub\Controller\Forum');
$fc = new $class($this->app(), $this->app()->request());

might work, but you really shouldn't do this :)

What exactly are you trying to achieve?
 
Last edited:
What exactly are you trying to achieve?
Creating a custom thread (e.g. not the standard thread) without having to worry about anything like permissions, validation, etc. The forum controller handels all of that within actionPostThread(), so extending that function is what I'm trying to do.
Edit: Ah, yea, and it's from an external site.
 
PHP:
$creator = $this->service('XF:Thread\Creator', $forum);
$creator->setContent($title, $message);
$creator->sendNotifications(); // if necesary
$thread = $creator->save();

This would be the preferred aproach for creating a thread automatically/non-inteactively.
 
Yep, that's what actionPostThread() does (it calls setupThreadCreate()), but there is no permission check at all in your code above afaik. I guess the best thing is just to copy actionPostThread() and modify that instead of fiddeling around with XFCP.
 
I've intentionally left out all the permission stuff as I thought your intetion was to not care about that :)

If you really want to do all that actionPostThread() does, you might want to look at
PHP:
$this->rerouteController('XF:Forum', 'postThread');

Or maybe override methods of the forum controller to do what you need, it depends on what exactly you want to achieve.
 
Back
Top Bottom