0

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

I would like to add option to admin panel for Disable/Enable the link on frontend's footer.

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,

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

At first add config to your module.
in file module/etc/adminhtml/system.xml add:

            <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1"
                   showInStore="1">
                <label>Enabled</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field> 

In section and group that you want to display it. It should be actually exist one for your module.

For getting value of this option you have to create Helper eg module/Helper/Admin/Config.php(or add only function if this is exist) and make constructor:

protected $_storeManager;

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager
)
{
    $this->_storeManager = $storeManager;
    parent::__construct($context);
}

then function:

/**
 * @return bool
 */
public function isActive()
{
    return $this->scopeConfig->getValue('your_section/your_group/active');
}

Almost done.

You have to just find where link is displaying and check what value if for active option.

  • I have added the config for my module. Also created the Helper. But I dont seem to find a way to diable/enable a footer link in frontend with the config in adminhtml. I am really new to magento 2 so please bear with me. – manini Apr 4 '17 at 8:46
  • Find in your module place where link is adding to footer. I can not help you with that at all cause It id custom module and I have no idea how it works and looks :c When You will find that place, Ill help You with problems – Konrad Siamro Apr 4 '17 at 9:04
  • Can u please have a look at the edited question once? – manini Apr 4 '17 at 9:57
0

Finally, I was successful to achieve this. Posting it in case it might help someone in future.

Created a helper.

/**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 */
protected $scopeConfig;

const XML_PATH_ENABLED = 'section/group/field';

public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) {
    $this->scopeConfig = $scopeConfig;
}

 /**
  * Sample function returning config value
  * */
public function getEnabled() {
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

    return $this->scopeConfig->getValue(self::XML_PATH_ENABLED, $storeScope);
}

Called this into my template file,

$helper = $this->helper('path/to/helper');
echo $value = $helper->getEnabled();

Also in view/frontend/layout/default.xml where I added the footer link, I was missing the ifconfig. Added the same.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.