XF 2.1 How do I extend any third party add-on class?

XDinc

Well-known member
Hello everyone!
How can I extend third party add-on class?

Example: Like as resource version we can use XFCP_ResourceVersion but if the add-on is third party how I can extend the class?

I used, as below
Code:
$class = \XF::extendClass('XF\Str\AnyAddon');

But the result was null.
 
I don't understand the question ... no matter if XF or an addon both in the same way. eg via ACP - Class extensions
 
I don't understand the question ... no matter if XF or an addon both in the same way. eg via ACP - Class extensions

Seeing you here is as good as getting answers.
Thank you for your support, I've already done this.
I need extend method! What is wrong? I've not found out yet

Code:
<?php

namespace MyAddon\Pub\Controller;

use XF\Mvc\ParameterBag;
use XF\Mvc\Reply\View;

class OtherAddonFile extends \AnyAddon\Pub\Controller\OtherAddonFile
{
  
    public function myaction(ParameterBag $params)
    {
      
        if ($response instanceof View)
        {

        }
      
        return $response;
    }
}
 
class OtherAddonFile extends XFCP_OtherAddonFile


Example from me

Code:
<?php

namespace AH\Tools\SV\ModeratorEssentials\Pub\Controller;

use SV\ModeratorEssentials\XF\Entity\ModeratorLog;
use XF\Entity\Forum;
use XF\Mvc\ParameterBag;
use XF\Pub\Controller\AbstractController;

class Moderator extends XFCP_Moderator
{
    public function actionThreadsolutionLog()
    {
    }
}
if(false)
{
    class XFCP_Moderator extends \SV\ModeratorEssentials\Pub\Controller\Moderator {}
}


Screenshot_1591003069.webp
 
Code:
<?php

namespace AH\Tools\SV\ModeratorEssentials\Pub\Controller;

use SV\ModeratorEssentials\XF\Entity\ModeratorLog;
use XF\Entity\Forum;
use XF\Mvc\ParameterBag;
use XF\Pub\Controller\AbstractController;

class Moderator extends XFCP_Moderator
{
    public function actionThreadsolutionLog()
    {
    }
}
if(false)
{
    class XFCP_Moderator extends \SV\ModeratorEssentials\Pub\Controller\Moderator {}
}

Could you explain this a little bit, because I'm just searching as well a solution on how to overwrite / overload a function of another addon . Why is there an if(false) ? I tried it but the code will not be executed.
 
Code:
if(false)
{
    class XFCP_Moderator extends \SV\ModeratorEssentials\Pub\Controller\Moderator {}
}
you can safely ignore :)


Example:

You want to overwite XF thread controller Methode actionIndex
Screenshot_1591557916.png

Xour File in Path src\MeinAddon\XF\Pub\Controller\Thread.php
Code:
<?php

namespace MeinAddon\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Thread extends XFCP_Thread
{
    public function actionIndex(ParameterBag $params)
    {
        print 'SYSTEM OVERWRITTEN SKYNET HAS WON!';
        exit;
    }
}

Screenshot_1591558351.png




Screenshot_1591558486.png
 
But this is for the Xenforo functions and classes, how does it work for a third party application/addon ? I would like to replace a complete function of that 3rd party addon.
 
Strange do not work, do I have to set the execution order to lower number ?

Both will be executed, my and still the 3rd party function
 
Sorry need to ask again, on some it is working but not on some others. The 3rd party addon has the code like that
PHP:
<?php
namespace 3rdP\Chicken\BbCode;

class Bbq
{
    public static function flip($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
    {
        if (!isset($options['entity']))
        {
            // This must be outside the Show Thread page, ignore it
            return $renderer->renderSubTree($tag['children'], $options);
        }
.
. some more code
.
        return $renderer->renderSubTree($tag['children'], $options);
    }
}

So in my addon I got:
PHP:
<?php

namespace myaddon\3rdP\Chicken\BbCode;

class Bbq extends XFCP_Bbq
{
    public static function flip($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
    {
        $result = parent::flip($tagChildren, $tagOption, $tag, array $options, $renderer);
.
.do some things here
.
        return $renderer->renderSubTree($tag['children'], $options);
    }
}
In the Class Extensions section I added:
Base Class: 3rdP\Chicken\BbCode\Bbq
Ext Class: myaddon\3rdP\Chicken\BbCode\Bbq

But when I reload now the forum page, my code will never be executed. Any ideas why?
Thx again
 
Last edited:
But when I reload now the forum page, my code will never be executed. Any ideas why?
it seems like your code is fine.
could you do make another new post with that BB code, and try hitting refresh?
try adding this line
\XF::dump('test');


next to this line in your add-on:
public static function flip($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)


Then hit refresh or re-post the bbbcode.

then again, add that same line into the third party add-on's code (hardcode) and see if it's running.
if the 3rd party add-on also not executing the code, there might have another issue in BBCode settings page in ACP (possibly failing regex matches)
 
Hi @Earl,

thanks for your answer, but if I place the code into my Addon it will not be executed, but if I place it into the 3rd party one i see the message. Anyway I use a debugging session with break points and I see that only the 3rd party code will be executed.
 
Hi @Earl,

thanks for your answer, but if I place the code into my Addon it will not be executed, but if I place it into the 3rd party one i see the message. Anyway I use a debugging session with break points and I see that only the 3rd party code will be executed.
Maybe that's because it's a BBCode callback static method and they are not extendable? I'm not so sure about it, I suggest you to create a brand new BBCode via Admin CP and point the PHP callback to your own static method, it would be possible if you don't use :self in your code. You don't even use parent:: do you? You said you completely overriding the callback method
 
Maybe that's because it's a BBCode callback static method and they are not extendable?
Great tip and yes it is a callback. So there is no way to extend this kind of callbacks ?
I use as well the parent:: call as you could see in the code above.

Thanks for your help.
 
So there is no way to extend this kind of callbacks ?

I use as well the parent:: call as you could see in the code above.
Well, You said you use debug. In that case, you can test it on your own by adding something before this parent:: line
$result = parent::flip($tagChildren, $tagOption, $tag, array $options, $renderer);
and add a breakpoint to it.

Then run the debug (refresh or create a post with the BBCode) and see if it catches the breakpoint. if it does not, it means parent:: line
$result = parent::flip($tagChildren, $tagOption, $tag, array $options, $renderer);
is not the line that gets called the parent:: method. Actually, you can check up on the stack when you are running and hit the breakpoint of the original add-ons method, you stay there and inspect the 'xdebug stack trace', and you will see who's calling that code.
if the caller is not your parent:: line, it means it directly get called from somewhere else because its a php callback.

another tip:
if the XenForo class proxy system to be worked, the base class has to be extended by this function in \XF::extendClass
Take a look at that code too, and do some experiments with it.
in most cases like $this->repository(), \XF::Repository() have this \XF:extendClass() method get used transparently.
 
Thanks again for your help.
The problem is, that this is a callback and if I understand you correctly, it is not possible to extend that? Right?

How can I call then the code of the third party addon from my code, if I enter the callback method under the BBCode to call my function?
 
it is not possible to extend that?
I can't give you a concrete answer because I haven't tested IT, and I feel like 95% positive that has to be the reason, but maybe not because it is a callback,

But I can confirm one thing. A class is extendable by the xenforo class proxy system only when this line gets executed \XF::extendClass


It's not extendable when the class gets instantiated by classname new(), and maybe, yeah, not when the method is static too. (I remember @Chris D has posted it somewhere but can't remember the static thing)

So... You'll have to create another new custom BB code? And copy the custom BB code settings from that 3rd party add-on's setting page, and disable that whole custom BB code of 3rd party addon from Admin CP, and use your own PHP callback to your own BBCode.

I don't know if there is another way to do this.
 
I take the whole code of the 3rd Party Addon and do the changes inside that one, then I changed the callback class to my one. Thx for all your help this is much appreciated.
 
I take the whole code of the 3rd Party Addon and do the changes inside that one, then I changed the callback class to my one. Thx for all your help this is much appreciated.
if you are using $result variable from
$result = parent::flip($tagChildren, $tagOption, $tag, array $options, $renderer);
and if it can prevent you from duplicating bunch of lines, you can still call the 3rd party add-ons code like this:
$result = 3rdP\Chicken\BbCode::flip($tagChildren, $tagOption, $tag, array $options, $renderer);

and do something with $result, as opposed to copying the whole the codes into your addon.
but still, have the PHP callback to yours.

I wouldn't recommend you to change 3rd party add-ons setting, I suggest you create your own custom BB code and disable the 3rd party BBCode
because it could break XenForo file health check, also it could cause errors when you upgrade 3rd party add-on to a new version.
 
Back
Top Bottom