XF 2.1 Creating an addon to extend functionality of another addon

Dannymh

Active member
Hi,

I am very rusty on my XF development but trying to whip up a few custom addons for my site before everything kicks off into our peak season.

The first I am trying to do adds additional functionality to another addon, basically it adds the ability to import data based on form attributes in admin as well as a few other minor things.

I had previously written this for XF 1.5 and now wanted to add the same thing here.
In XF 1.5 I would create an admin template modification to add a button and add a Code Event Listener for "load_class_controller" which pointed to a listener.php file with a class and function that would look something like

Code:
public static function loadClassListener($class, &$extend)
 {
                if ($class == 'someaddon_ControllerAdmin_Season')
                {
                        $extend[] = 'Silvertails_someaddon_ControllerAdmin_someaddonImporter';
                }
}

That would then point me out to my extension and let me work my extension code. For the life of me under XF2.1 I could not find any way of doing this. Reading the tutorial has me a little confused and just wondering if someone can point me in the right direction here?

I have my link in and I tried adding a class extension but i still cant get it to trigger anything if ends up erring as it seems to be also trying to point to something else.

Hopefully someone can give me a basic logic of something like
1 - Add code extension to look for x and point to y
2 - add code event listener x to y
3 - write your code

Not sure why my brain is not computing this and apologise for the probably basic question

Dan
 
In most cases you can simply use a class extension - your class will be a subclass of the extended class and you can over-ride any public or protected functions.

No need for listeners.
 
In most cases you can simply use a class extension - your class will be a subclass of the extended class and you can over-ride any public or protected functions.

No need for listeners.
Thanks, I think I know where I erred will check when I get home shortly
 
Of a class extension?

Have you read the docs? https://xenforo.com/xf2-docs/dev/general-concepts/#extending-classes

For a real example of a basic class extension, my Geoblock Registration addon shows how it works:


Hampel\Geoblock\XF\Service\User\Registration extends XF\Service\User\Registration

PHP:
<?php namespace Hampel\Geoblock\XF\Service\User;

class Registration extends XFCP_Registration
{
    // override the Registration::checkForSpam() function
    public function checkForSpam()
    {
        parent::checkForSpam(); // start by calling the parent function from the original class

        $this->geoblockCheckEu(); // call our extra functionality
    }

    public function geoblockCheckEu()
    {
        // do some stuff in addition to what was in the original class
        // ...
    }
}
 
Of a class extension?

Have you read the docs? https://xenforo.com/xf2-docs/dev/general-concepts/#extending-classes

For a real example of a basic class extension, my Geoblock Registration addon shows how it works:


Hampel\Geoblock\XF\Service\User\Registration extends XF\Service\User\Registration

PHP:
<?php namespace Hampel\Geoblock\XF\Service\User;

class Registration extends XFCP_Registration
{
    // override the Registration::checkForSpam() function
    public function checkForSpam()
    {
        parent::checkForSpam(); // start by calling the parent function from the original class

        $this->geoblockCheckEu(); // call our extra functionality
    }

    public function geoblockCheckEu()
    {
        // do some stuff in addition to what was in the original class
        // ...
    }
}
Thank.you that helps a lot
 
Of a class extension?

Have you read the docs? https://xenforo.com/xf2-docs/dev/general-concepts/#extending-classes

For a real example of a basic class extension, my Geoblock Registration addon shows how it works:


Hampel\Geoblock\XF\Service\User\Registration extends XF\Service\User\Registration

PHP:
<?php namespace Hampel\Geoblock\XF\Service\User;

class Registration extends XFCP_Registration
{
    // override the Registration::checkForSpam() function
    public function checkForSpam()
    {
        parent::checkForSpam(); // start by calling the parent function from the original class

        $this->geoblockCheckEu(); // call our extra functionality
    }

    public function geoblockCheckEu()
    {
        // do some stuff in addition to what was in the original class
        // ...
    }
}

Not sure what is going on here but if I do as you have there (this is for an Admin class)
I get
Code:
Fatal error: Class 'Silvertails\anaddon\XAddon\addon\Admin\Controller\Import\XFCP_Season' not found in /var/www/public/Silvertails/src/addons/Silvertails/anaddon/XAddon/addon/Admin/Controller/Import.php on line 6

if I change my code from

Code:
class Import extends XFCP_Season
{

to

Code:
class Import
{


I get an error in the interface of

Code:
Exception: Could not find class Silvertails\anaddon\XAddon\addon\Admin\Controller\Import when attempting to extend XAddon\anaddon\Admin\Controller\Season in src/XF/Extension.php at line 174

All my paths are correct but seems I can't get it to trigger
 
Tweaking a bit is giving me "invalid_action" errors and pointing to the parent class rather than my extended class
 
It should be class Import extends XFCP_Import - it's a magic class name used by the class extension system
 
OMG just realised something else i was doing that was just moronic. Starting to make sense now and at least have my page loading so just need to add my code now.

Thanks again for your help, I think I have what I need to get the task sorted again.
 
  • Like
Reactions: Sim
Top Bottom