How to get system.xml config value in layout xml Magento 2 - Kishan Savaliya

Welcome to Kishan Savaliya's Magento 2 Blog.In this blog post, I will explain how to get system.xml configuration value in layout xml file in Magento 2. If you're developing any theme or extension and based on configuration you want to enable disable any blocks, links or if you want to provide admin configuration where admin user can change Label or Path of any particular link, then this article can be useful to you.

So let's start how you can get config value in layout XML file. You can follow below given steps.

I'm assuming, you have your own custom module, so navigate inside your module first.

1.) Create system.xml config file

app/code/SK/Module/etc/adminhtml/system.xml

Content for this file is..

<?xml version="1.0"?>
<!--
/**
 * @package   SK\Module
 * @author    Kishan Savaliya <kishansavaliyakb@gmail.com>
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
    <system>
        <tab id="sk_tab" translate="label" sortOrder="15">
            <label><![CDATA[SK Tab]]></label>
        </tab>
        <section id="sk_section" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Configuration</label>
            <tab>sk_tab</tab>
            <resource>SK_Module::config</resource>

            <!-- SK Config Group -->
            <group id="sk_group" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>SK Config Group</label>
                <field id="enable_link" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Link?</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <comment>sk_section/sk_group/enable_link</comment>
                </field>
                <field id="label_of_custom_link" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Label of Custom Link</label>
                    <comment>sk_section/sk_group/label_of_custom_link</comment>
                </field>
                <field id="path_of_custom_link" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Path of Custom Link</label>
                    <comment>sk_section/sk_group/path_of_custom_link</comment>
                </field>
            </group>
            <!-- SK Config Group -->
        </section>
    </system>
</config>

2.) Now create any layout file and use config value.

I'm creating default.xml layout file and I will use above config values there.

app/code/SK/Module/view/frontend/layout/default.xml

Content for this file is..

<?xml version="1.0"?>
<!--
/**
 * @package   SK\Module
 * @author    Kishan Savaliya <kishansavaliyakb@gmail.com>
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
      <referenceBlock name="header.links">
         <block class="Magento\Framework\View\Element\Html\Link" name="new-custom-link" ifconfig="sk_section/sk_group/enable_link">
            <arguments>
               <argument name="label" translate="true" xsi:type="helper" helper="SK\Module\Helper\Data::getLabelOfCustomLink" />
               <argument name="path" translate="true" xsi:type="helper" helper="SK\Module\Helper\Data::getPathOfCustomLink" />
            </arguments>
         </block>
      </referenceBlock>
   </body>
</page>

Here in above file I used ifconfig this will use value of our system configuration from this path sk_section/sk_group/enable_link, if this setting is enabled in backend then only we can see that custom link in header links section and if that is disabled then we can not see that link there. I also used xsi:type="helper" in arguments of label and path and in helper attribute I passed Helper class name and function name for from where to get Label name and where to get path of that custom link, so now I will create helper file and I will return system configuration value of label and path. So let's create Helper file here..

3.) Create Helper file and return config value.

app/code/SK/Module/Helper/Data.php

Content for this file is..

<?php
/*
 * @package   SK\Module
 * @author    Kishan Savaliya <kishansavaliyakb@gmail.com>
 */

namespace SK\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    const CONFIG_LABEL = 'sk_section/sk_group/label_of_custom_link';
    const CONFIG_PATH = 'sk_section/sk_group/path_of_custom_link';

    protected $scopeConfig;

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

    public function getLabelOfCustomLink(){
        return $this->getConfig(self::CONFIG_LABEL);
    }

    public function getPathOfCustomLink(){
        return $this->getConfig(self::CONFIG_PATH);
    }

    public function getConfig($configPath)
    {
        return $this->scopeConfig->getValue(
            $configPath,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

That's it. Now you can set config value in backend and clear cache of Magento and check link in your storefront.

How to get system.xml config value in layout xml Magento 2

How to get system.xml config value in layout xml Magento 2


Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.

If you enjoyed this blog post, share it with friends!