XF 2.1 How to detect if an add-on is installed

This is how to do it in PHP:
PHP:
$addOns = \XF::app()->container('addon.cache');
if (array_key_exists('Some/Id', $addOns) && $addOns['Some/Id'] >= 1010031)
{
    // Your code goes here
}

I'm not 100% sure if this is possible in the templates, but I don't think so since template functions have to have a prefix like "get", "is", "has" (implying only checking or fetching data, not writing data. Security reasons and all.)

A workaround would be for you to create an AddOn repository where you could create a method like
PHP:
public function hasAddOn($addOnId, $atLeastVersion = null)
{
    $addOns = \XF::app()->container('addon.cache');
    if (!array_key_exists($addOnId, $addOns))
    {
        return false;
    }

    if ($atLeastVersion !== null && $addOns[$addOnId] < $atLeastVersion)
    {
        return false;
    }

    return true;
}

Then use the following in your template code:
HTML:
<xf:set var="$addOnRepo" value="{{ $xf.app.em.getRepository('MattC\SomeAddon:AddOn') }}" />
<xf:if is="$addOnRepo.hasAddOn('Some/Id', 1010031)">
    AddOn "Some/Id" is installed and at least version 1.1.0 Beta 1
</xf:if>

There may be a built-in method for this that I'm not aware of but I couldn't find one from a brief search.

(Edit: Just a wee note to clarify that the forward slashes are intended here, though normally you refer to add-on IDs with backslashes like in the getRepository call. The addon.cache container stores add-on IDs with forward slashes, so that's what we must use.)

Hope this helps!


Fillip
 
This is how to do it in PHP:
PHP:
$addOns = \XF::app()->container('addon.cache');
if (array_key_exists('Some/Id', $addOns) && $addOns['Some/Id'] >= 1010031)
{
    // Your code goes here
}

I'm not 100% sure if this is possible in the templates, but I don't think so since template functions have to have a prefix like "get", "is", "has" (implying only checking or fetching data, not writing data. Security reasons and all.)

A workaround would be for you to create an AddOn repository where you could create a method like
PHP:
public function hasAddOn($addOnId, $atLeastVersion = null)
{
    $addOns = \XF::app()->container('addon.cache');
    if (!array_key_exists($addOnId, $addOns))
    {
        return false;
    }

    if ($atLeastVersion !== null && $addOns[$addOnId] < $atLeastVersion)
    {
        return false;
    }

    return true;
}

Then use the following in your template code:
HTML:
<xf:set var="$addOnRepo" value="{{ $xf.app.em.getRepository('MattC\SomeAddon:AddOn') }}" />
<xf:if is="$addOnRepo.hasAddOn('Some/Id', 1010031)">
    AddOn "Some/Id" is installed and at least version 1.1.0 Beta 1
</xf:if>

There may be a built-in method for this that I'm not aware of but I couldn't find one from a brief search.

(Edit: Just a wee note to clarify that the forward slashes are intended here, though normally you refer to add-on IDs with backslashes like in the getRepository call. The addon.cache container stores add-on IDs with forward slashes, so that's what we must use.)

Hope this helps!


Fillip
Hello,

I have an add-on that add the number of resources/media/albums but if you don't have XFMG & XFRM it didn't work. So how to make a condition in PHP ? I tried this one :
PHP:
        $activeAddons = \XF::app()->container('addon.cache');

        if (array_key_exists('XFRM', $activeAddons))
        {
            $forumResources = $this->db()->fetchOne("
            SELECT COUNT(resource_id)
            FROM xf_rm_resource
            ");
        }

But it doesn't work either, thank you !

Regards, SyTry

EDIT : solved :
 
Last edited:
Top Bottom