Get Config Value in Layout XML Magento 2

In this tutorial, I will explain to you how to get config value in the layout XML file in Magento 2. There are sometimes needs to set value in layout XML file from system config which admin set value in system configuration from admin panel. You can do this through a few steps.

 

Step 1:

Create system.xml file for add field to set value on /app/code/Magepow/Custom/etc/adminhtml/ path :

 

File: /app/code/Magepow/Custom/etc/adminhtml/system.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="Magepow" translate="label" sortOrder="10">
        <label>Magepow</label>
    </tab>
    <section id="custom" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
        <class>separator-top</class>
        <label>Magepow Custom</label>
        <tab>magepow</tab>
        <resource>Magepow_Custom::custom_config</resource>
        <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>General Configuration</label>
            <field id="label_field" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Label</label>
            </field>
            <field id="path_field" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Path</label>
            </field>
        </group>
    </section>
</system>

Step 2:

Create default.xml file at /app/code/Magepow/Custom/view/frontend/layout/ with this below content :

 

File: /app/code/Magepow/Custom/view/frontend/layout/default.xml

<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="custom-link-header">
                <arguments>
                    <argument name="label" xsi:type="helper" helper="Magepow\Custom\Helper\Data::getLabel" translate="true"/>
                    <argument name="path" xsi:type="helper" helper="Magepow\Custom\Helper\Data::getPath" translate="true"/>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>
 
Step 3:
Create Data.php file in Helper at /app/code/Magepow/Custom/Helper/ forget the value of system config with this below content :
 
File: /app/code/Magepow/Custom/Helper/Data.php
<?php

namespace Magepow\Custom\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper {

    const LABEL_FIELD = 'custom/general/label_field';

    const PATH_FIELD = 'custom/general/path_field';

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

    /**
     * @param \Magento\Framework\App\Helper\Context   $context
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ){
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

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

    public function getPath() {
        return $this->scopeConfig->getValue(self::PATH_FIELD,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
}
Step 4:

Now refresh Magento cache

php bin/magento cache:flush

 

Finally. Hope this article helps you in some way.