• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

how to make an add-on to overwrite an exist method ?!

TBDragon

Active member
#1
hi every one

i just have in mind to make an add-on to sort the RM categories

well the method i need to overwrite is


XenResource_Model_Category
getAllCategories()

i change the code manually to this

PHP:
        return $this->fetchAllKeyed('
            SELECT resource_category.*
                ' . $joinOptions['selectFields'] . '
            FROM xf_resource_category AS resource_category
            ' . $joinOptions['joinTables'] . '
            ORDER BY resource_category.category_title
        ', 'resource_category_id');
but for future when upgrading i will need to do that again also =(

so i was thinking of making an add-on to do an overwriting for that method

but i didnt know how to do it as i have low experience with XF development ^^


thanks for help
 
Last edited:

Chris D

XenForo developer
Staff member
#2
Extend the class that the method is in, and give the method the same name. It will overwrite the existing method instead of extending it.
 

TBDragon

Active member
#3
Extend the class that the method is in, and give the method the same name. It will overwrite the existing method instead of extending it.
u mean like this right ?!

PHP:
class TBDragon_RmSortingCategories_Category extends XenResource_Model_Category
{

    public function getAllCategories(array $fetchOptions = array())
    {
        $joinOptions = $this->prepareCategoryFetchOptions($fetchOptions);

        return $this->fetchAllKeyed('
            SELECT resource_category.*
                ' . $joinOptions['selectFields'] . '
            FROM xf_resource_category AS resource_category
            ' . $joinOptions['joinTables'] . '
            ORDER BY resource_category.category_title
        ', 'resource_category_id');
    }

if yes then its ok but what next

i make an addon and go to make Listener so i can let XF know from where it call the code

but i lost there =_= dono which Listener i should use !!
 

Chris D

XenForo developer
Staff member
#4
Basically, follow this tutorial:

http://xenforo.com/community/threads/how-to-add-more-actions-to-an-existing-controller.11202/

Instead of using the load_class_controller event, use the load_class_model event.

Then, basically, yes, this would overwrite that method:

PHP:
    public function getAllCategories(array $fetchOptions = array())
    {
        $joinOptions = $this->prepareCategoryFetchOptions($fetchOptions);

        return $this->fetchAllKeyed('
            SELECT resource_category.*
                ' . $joinOptions['selectFields'] . '
            FROM xf_resource_category AS resource_category
            ' . $joinOptions['joinTables'] . '
            ORDER BY resource_category.category_title
        ', 'resource_category_id');
    }
 

TBDragon

Active member
#6
hi @Chris D there is a small issue now

the addon work fine but its work also in the ACP
i mean its order the categories in admin.php?resource-categories/ and some how its miss as i dono where is the child and the parent :(
so is there way to exclude or stop working at ACP and just let it work and sort the categories in the forum ?!

thanks
 

Chris D

XenForo developer
Staff member
#7
Nope, Models are generally unaware of where they are being called from.

The solution would be to extend the methods in the controllers which call the overwritten method.
 

TBDragon

Active member
#8
Nope, Models are generally unaware of where they are being called from.

The solution would be to extend the methods in the controllers which call the overwritten method.
so what i understand

to let the addon Listener load the controller and this controller call the overwritten method ?!

if yes then how i can know the controller for the RM ?! and if no how to do it then ?!

waiting u and thanks