XF 2.0 Extending AddOn classes

Jake B.

Well-known member
I have some code that needs to run on install of certain other add-ons, and am trying to find the best class to extend to accomplish this. From what I can tell \XF\Addon\AddOn isn't run through XFCP so I can't extend that, and same goes for the Setup classes for each add-on. I even tried extending \XF\Job\AddOnData but that didn't seem to work either
 
No, haven't looked at controllers really as I need it to work any time the add-on is installed (CLI, or through AdminCP) so I'd like to avoid that -- especially once @Xon finishes his add-on installer
 
  • Like
Reactions: Xon
I actually tried that as well, but I need it to run after the tables have been created as I'm adding some new columns to these tables (for XFRM and XFMG specifically)
 
Seems that solution isn't super reliable, still having issues with these fields not being added when the add-on is installed. I could add a button that you have to press manually, but would really like to avoid that. What do you think would be the best way to accomplish this @Chris D?

Currently I have this:
PHP:
<?php

namespace ThemeHouse\Trending\XF\Entity;

class AddOn extends XFCP_AddOn
{
    protected function _postSave()
    {
        parent::_postSave();

        if ($this->isInsert()) {
            \XF::runOnce('addAddOnColumns', function() {
                /** @var \ThemeHouse\Trending\Repository\Trending $repo */
                $repo = $this->repository('ThemeHouse\Trending:Trending');
                $repo->setupColumnsForAddon($this->addon_id);
            });
        }
    }
}

which runs this:

PHP:
public function setupColumnsForAddon($addOnId)
{
    $schemaManager = $this->app()->db()->getSchemaManager();

    if ($addOnId === 'XFRM') {
        $schemaManager->alterTable('xf_rm_resource', function(\XF\Db\Schema\Alter $table) {
            $table->addColumn('thtrending_positive_rating_count', 'int')->setDefault(0);
            $table->addColumn('thtrending_positive_ratings_per_minute', 'decimal', '20,6')->setDefault(0);
        });
    }

    if ($addOnId === 'XFMG') {
        $schemaManager->alterTable('xf_mg_media_item', function(\XF\Db\Schema\Alter $table) {
            $table->addColumn('thtrending_positive_rating_count', 'int')->setDefault(0);
            $table->addColumn('thtrending_views_per_minute', 'decimal', '20,6')->setDefault(0);
            $table->addColumn('thtrending_positive_ratings_per_minute', 'decimal', '20,6')->setDefault(0);
            $table->addColumn('thtrending_positive_ratings_views_per_minute', 'decimal', '20,6')->setDefault(0);
        });
    }
}

However, it doesn't seem to be triggering correctly for everyone
 
Just wanting to bump this, it's causing several tickets a week even though we do have an option documented to go in and add these fields after XFMG/XFRM have been installed. Would really like a way to eliminate this issue :\
 
You might be better off creating your own tables and extending the appropriate entity classes in XFRM and XFMG in your add-on.

Then just do a check if the add-ons are installed to run the appropriate code. IE:
Code:
$addOns = \XF::app()->container('addon.cache');
$galleryInstalled = false;

if(array_key_exists('XFMG', $addOns))
{
    // CHECK VERSION
    if($addOns['XFMG'] >= 902000470) $galleryInstalled = true;
}

if($galleryInstalled)
{
    .... your code ....
}

Your tables existing won't hurt anything, the class extensions just won't run unless the XFRM/XFMG are installed and your code won't execute unless the add-ons are installed.
 
We'd rather keep these fields within the content table as they're fairly heavily used for sorting/filtering and such. Ideally there should be a way I can add something to an add-on's tables like I can for default XenForo without having to require users to go press a button somewhere
 
Using Snog's method, you could join in your entity with relations. That should be fine for filtering/sorting stuff.
Otherwise, if you could be more specific on
However, it doesn't seem to be triggering correctly for everyone
that would be helpful.
 
Top Bottom