Search code examples
frontendmagento2adminhtml

Disable/Enable custom frontend footer link from admin panel in magento 2


I have a custom module that displays a custom link in the footer section in frontend. Then, I have my module specific configurations in the admin panel.

I would like to Disable/Enable the link on frontend's footer through the configuration in admin panel.

How can I achieve this? Please help.

I have added the footer link in frontend in view/frontend/layout/default.xml

    <referenceBlock name="footer_links">
    <block class="Magento\Framework\View\Element\Html\Link\Current" name="Name">
        <arguments>
            <argument name="label" xsi:type="string">My custom link</argument>
            <argument name="path" xsi:type="string">path/to/link</argument>
        </arguments>
    </block>

My system.xml is like,

    <!--For creating section -->
<section id="quotes" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Module</label>

    <!--Assign section to tab -->
    <tab>mytab</tab>
    <resource>Vendor_Module::configuration</resource>

    <!--create group for fields in section -->
    <group id="quotes" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Group Name</label>

        <!--create text type field -->
        <field id="enabled" translate="label comment" sortOrder="7" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Enable the link</label>
            <comment>select from the dropdown</comment>

            <!-- source model which we created for drop down options -->
            <source_model>Vendor\Module\Model\Config\Source\Module</source_model>
        </field>
    </group> 
</referenceBlock>   

The Vendor\Module\Model\Config\Source\Module contains,

public function toOptionArray() {
return [
    ['value' => 'Yes', 'label' => __('Yes')],
    ['value' => 'No', 'label' => __('No')],
 ];
}

Solution

  • Please use block as mentioned below to Disable/Enable the link on frontend's footer:

    <block ifconfig="quotes/quotes/enabled" class="Magento\Framework\View\Element\Html\Link\Current" name="Name">
        <arguments>
            <argument name="label" xsi:type="string">My custom link</argument>
            <argument name="path" xsi:type="string">path/to/link</argument>
        </arguments>
    </block>
    

    Quotes field should be drop down with YES and NO. When the quotes field will be YES then it will show otherwise it will not be shown.