In Magento 2, we can use ifconfig in layout XML for the block to display or not based on the Magento / Config / Model / Config / Source / Yesno configuration field by using the ifconfig argument.
Basically, it’s a condition in that block, which determines whether the block content is going to be rendered or not.
Syntax :
<block class="Namespace\Module\Block\Type" ifconfig="section/group/field" name="block.ifconfig.example">
...
</block>
Here, in the block we have specified ifconfig=”section/group/field“, if the system config returns boolean value true then the block will be rendered only.
Let’s see and understand the below example :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="footer_links">
<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="contact/contact/enabled" name="contact-us-link">
<arguments>
<argument name="label" xsi:type="string" translate="true">Contact Us</argument>
<argument name="path" xsi:type="string">contact</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
Here in the above example, if the contact form is enabled from the store configuration then only the footer will show the Contact Us link.
Using this approach you can use ifconfig in layout XML to make block dynamic based on store configurations.
You may also like :
How to Get Store Configuration value in Magento 2?
How to get value from env.php in Magento 2?
That’s it for this tutorial, Happy Coding 🙂
Leave a Comment