How to Get Config Value in Layout XML in Magento 2?

Today, we’re going to teach you how to get config value in layout XML in Magento 2.

Generally, there are static text or value assign in the layout XML.

But, many times you’ll need to set value in layout XML file from system config in order to set admin value in system configuration from the admin panel.

Also read: How to Get Value from Store Configuration by Scope in Magento 2?

And in this tutorial, we’ll show to exactly how to set value dynamically and get config value in layout XML.

Steps to Get Config Value in Layout XML

Before we get started with steps, you need to create a simple module. Now, we’ve already created a separate tutorial on How to Create a Simple Module in Magento 2.

Once you’ve created a simple module, it’s time to get started!

Step #1

First of all, we need to create a system.xml file in app/code/MD/Helloworld/etc/adminhtml/ directory to add field to set value.

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="md" translate="label" sortOrder="10">
            <label>MD</label>
        </tab>
        <section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>MD Helloworld</label>
            <tab>md</tab>
            <resource>MD_Helloworld::helloworld_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>
</config>

Step #2

Now, we will create a default.xml in app/code/MD/Helloworld/view/frontend/layout directory and add the following code.

<?xml version="1.0"?>
<!-
/**
 * Copyright © Magento, Inc. 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="header.links">
            <block class="Magento\Framework\View\Element\Html\Link" name="custom-link-header">
                <arguments>
                    <argument name="label" xsi:type="helper" helper="MD\Helloworld\Helper\Data::getLabel" translate="true"/>
                    <argument name="path" xsi:type="helper" helper="MD\Helloworld\Helper\Data::getPath" translate="true"/>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Step #3

Next, we will need to create a Data.php file in the Helper directory at app/code/MD/Helloworld/Helper/ to get the value of system config using the following code.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace MD\Helloworld\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper {   
    const LABEL_FIELD = 'helloworld/general/label_field';
    const PATH_FIELD = 'helloworld/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);
    }
}

And it’s done!

Now, you can add the value of path and label from configuration and save the value.

After that, clean the cache and check output at the front-end and you’ll see the link in header that you’ve set from the admin panel.

Also read: How to Get Store Config Value in Magento 2?

Concluding Thoughts…

So, this is how you can get config value in layout XML in Magento 2. In addition, you can also get value from admin using xsi:type-=”helper” from any XML file and get value from the helper file.

With this we’ve come to the end of this tutorial. We hope that you found this tutorial helpful and learned how to get config value in layout XML in Magento 2.

If you’ve any queries, please share them in the comments below.

And if you need our professional assistance, feel free to contact us.