2

I have added a custom field in system->configuration. The code is below.

<tab id="testtab">
            <label>Test settings</label>
        </tab>
        <section id="testsection" type="text" showInDefault="1">
            <label>Test Configuration</label>
            <tab>testtab</tab>
            <resource>Namespace_Modulename::system_config</resource>
            <group id="settings" type="text"  sortOrder="10" showInDefault="1">
                <label>Credentials</label>
                <field id="key" translate="label" type="text" sortOrder="30" showInDefault="1">
                    <label>Key</label>
                </field>
            </group>
        </section>

Now I am trying to get the data from cms page like this

{{config path="testsection/settings/key"}}

But I cant access the value using that. Any ideas?

4
  • does it work for values that existed previously?
    – Marius
    Commented Jun 26, 2017 at 12:24
  • Well i didnt check that. But if i add fields after clicking on "add variables" button then it works. Commented Jun 26, 2017 at 12:25
  • Then post your finding as an answer?
    – Marius
    Commented Jun 26, 2017 at 12:57
  • @Marius also whenever i am trying to save it its getting changed to something like this <div class="new class" data-sitekey="domain.com/admin/cms/wysiwyg/directive/___directive/e3tjdXN0b21WYXIgY29kZT1zaXRlX2tleX19/key/5a44f2ce7bbac0b884cde380826ded9c1448ad0957242f6c3b914e0489d95c67/"></div> where data-sitekey holds the field value. Its weird. Commented Jun 26, 2017 at 12:58

4 Answers 4

5

In Magento 2 you can only use a small set of config paths in the {{config}} directive in blocks or pages.

The story behind it (scroll lower for solution):
The class that handles the {{...}} directives for cms blocks and pages is Magento\Widget\Model\Template\Filter. This one extends Magento\Email\Model\Template\Email.
In this last class there is this method that handles {{config}} directives

public function configDirective($construction)
{
    $configValue = '';
    $params = $this->getParameters($construction[2]);
    $storeId = $this->getStoreId();
    if (isset($params['path']) && $this->isAvailableConfigVariable($params['path'])) {
        $configValue = $this->_scopeConfig->getValue(
            $params['path'],
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $storeId
        );
    }
    return $configValue;
}

In order to use a certain config path, the method isAvailableConfigVariable needs to return true

This method looks like this:

private function isAvailableConfigVariable($variable)
{
    return in_array(
        $variable,
        array_column($this->configVariables->getData(), 'value')
    );
}

so it checks if the path you send is returned by \Magento\Email\Model\Source\Variables::getData.

That's why only a small set of config paths can be used.

Now the solution
Create an after plugin for the method \Magento\Email\Model\Source\Variables::getData

Add this to the di.xml of one of your modules

<type name="Magento\Email\Model\Source\Variables">
    <plugin name="add-test-credentials" type="[Vendor]\[Module]\Plugin\Model\Source\VariablePlugin" />
</type>

and create the plugin class:

<?php
namespace [Vendor]\[Module]\Plugin\Model\Source;

use Magento\Email\Model\Source\Variables;

class VariablePlugin
{
    /**
     * @param Variables $subject
     * @param $data
     * @return array
     */
    public function afterGetData(Variables $subject, $data)
    {
        $data[] = [
            'value' => 'testsection/settings/key',
            'label' => __('Credentials for test settings') //give it a label that suits you best. This will be listed in the insert custom var popup in the cms pages and blocks (see image below)
        ];
        return $data;
    }
}

insert custom variable to block or page

4

This code is tested in Magento 2.4. it is working fine.

You need to add a variables in the di.xml

[Vendor]/[Module]/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Variable\Model\Source\Variables">
        <arguments>
            <argument name="configPaths" xsi:type="array">
                <item name="[Section_Id]/[Group_Id]" xsi:type="array">
                    <item name="[Section_Id]/[Group_Id]/[Field_Id_1]" xsi:type="string">1</item>
                    <item name="[Section_Id]/[Group_Id]/[Field_Id_2]" xsi:type="string">1</item>
                    <item name="[Section_Id]/[Group_Id]/[Field_Id_3]" xsi:type="string">1</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

You can add following line in CMS page or blocks

{{config path='Section_Id/Group_Id/Field_Id_1'}}

{{config path='Section_Id/Group_Id/Field_Id_2'}}

{{config path='Section_Id/Group_Id/Field_Id_3'}}

1
  • 1
    This appears to be the correct answer although type Magento\Variable\Model\Source\Variables didn't work for me (in Magento 2.4.3), I used this: Magento\Variable\Model\Config\Structure\AvailableVariables. Works as expected
    – Zankar
    Commented Nov 15, 2021 at 12:41
3

You could also add your field in the di.xml file, then it will be available in your email templates.

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Variable\Model\Source\Variables"> <arguments> <argument name="configPaths" xsi:type="array"> <item name="general/store_information" xsi:type="array"> <item name="general/store_information/custom_config" xsi:type="string">1</item> </item> </argument> </arguments> </type> </config>

In email template :

{{config path="general/store_information/custom_config"}}

Magento does it in vendor/magento/module-variable/etc/di.xml

2

If for some reasons previous answers like this https://magento.stackexchange.com/a/325492/86806 doesn't work for you can try like this:

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Variable\Model\Config\Structure\AvailableVariables">
        <arguments>
            <argument name="configPaths" xsi:type="array">
                <item name="[Section_Id]/[Group_Id]" xsi:type="array">
                    <item name="[Section_Id]/[Group_Id]/[Field_Id_1]" xsi:type="string">1</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

I change <type name="Magento\Variable\Model\Source\Variables"> on <type name="Magento\Variable\Model\Config\Structure\AvailableVariables"> I tested it in magento 2.4.6-p2

1
  • 1
    I'm on Magento 2.4.6-p2 and this was exactly what I was looking for, and it works! Thank you!
    – dossy
    Commented Dec 30, 2023 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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