XF 2.3 Conditional

Ozzy47

Well-known member
Does anyone know if there is a conditional (to use in templates) to check if an addon is installed?
 
I think it's possible, that you can use is_addon_active() function on your templates.

HTML:
<xf:if is_addon_active('Elf/AddonTitle')>
    <!-- Content to show when AddonTitle is installed -->
    AddonTitle is installed!
<xf:else />
    <!-- Content to show when AddonTitle is not installed -->
    AddonTitle is not installed.
</xf:if>


You can also use the exact addon ID to appears in the addon.json file. Format is typically VendorPrefix/AddonName.

Alternativve you can check for specific addon options if you want to verify not just installation but specific functionality,
HTML:
<xf:if exists($xf.options.elf_addontitle_someoption)>
    <!-- Content that depends on a specific AddonTitle option -->
</xf:if>
 
Less:
<xf:if is="is_addon_active('Elf/AddonTitle')">

That did not give a syntax error like the previous examples so that's cool. But I went with doing it in PHP then calling it in my template.

PHP:
        $options = \XF::options();
        
        // Check If Conversation View Addon Is Installed (so we can link to the convo and message in our log)
        $ozzmodzPmDmSearch = isset($options->ozzmodz_pmdmsearch_limit) && $options->ozzmodz_pmdmsearch_limit;

        $viewParams = [
            'ozzmodzPmDmSearch' => $ozzmodzPmDmSearch,
        ];

Then in my template:
HTML:
                        <xf:if is="$ozzmodzPmDmSearch">
                            <xf:cell>{$this}</xf:cell>
                        <xf:else />
                            <xf:cell>{$that}</xf:cell>
                        </xf:if>
 
That did not give a syntax error like the previous examples so that's cool. But I went with doing it in PHP then calling it in my template.

PHP:
        $options = \XF::options();
       
        // Check If Conversation View Addon Is Installed (so we can link to the convo and message in our log)
        $ozzmodzPmDmSearch = isset($options->ozzmodz_pmdmsearch_limit) && $options->ozzmodz_pmdmsearch_limit;

        $viewParams = [
            'ozzmodzPmDmSearch' => $ozzmodzPmDmSearch,
        ];

Then in my template:
HTML:
                        <xf:if is="$ozzmodzPmDmSearch">
                            <xf:cell>{$this}</xf:cell>
                        <xf:else />
                            <xf:cell>{$that}</xf:cell>
                        </xf:if>
If the check is only needed in the template, it seems logical to me to call the is_addon_active method from the template. But if you prefer to check in PHP, it is better to check if the addon is available using the \XF::isAddOnActive() method, because you are checking for the presence and content of options, and they will be available even if the addon is disabled, which will lead to unexpected results.
 
If the check is only needed in the template, it seems logical to me to call the is_addon_active method from the template. But if you prefer to check in PHP, it is better to check if the addon is available using the \XF::isAddOnActive() method, because you are checking for the presence and content of options, and they will be available even if the addon is disabled, which will lead to unexpected results.

Yes, that seems to work as expected, thank you.
 
Back
Top Bottom