How to implement a dynamically generated comment for a backend configuration field?

backend
configuration

(Dmitry Fedyuk) #1

1. Documentation

In order to use the functionality follow these steps:

  1. create adminhtml/system.xml file in the Magento module
  • reference or create some section and group of the system config
  • add field with the all the required nodes. Add following sub-node to the field<comment model=“ModelFQN” … />
  • create ModelFQN which implements Magento\Config\Model\Config\CommentInterface.
    Return string out of getCommentText() which will show up in the comment section.

Example:

system.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="dev">
            <group id="debug">
                <field id="graphql_enabled" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Sample</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <comment model="Magento\Sample\Model\Comment" />
                </field>
            </group>
        </section>
    </system>
</config>

Model:

namespace Magento\Sample\Model;
use \Magento\Config\Model\Config\CommentInterface;
class Comment implements CommentInterface
{
    public function getCommentText($elementValue)  //the method has to be named getCommentText
    {
        //do some calculations here
        return $elementValue . 'Some string based on the calculations';
    }
}

2. Usage

The core modules never use it.

3. Availability

The system.xml schema allows the model attribute since Magento 2.1.0.

It is not available in the Magento 2.0.x versions (even in the latest Magento 2.0.14)

4. Implementation

It is possible because of the Magento\Config\Model\Config\Structure\Element\Field::getComment() implementation:




(Mark Davidson) #2

looked at Config/Reader that is responsible for system.xml file, and do not find an existing way to do this (however maybe Im wrong).

It is possible to use dynamic things ONLY in node ATTRIBUTES (As i understand the code). And each dynamic variable for attribute should look like. However you can try to use this format: “section_name/group_name/field_name” instead of “[module]/field_comment”:

.... However you can add your own compiler which allows to do this: You can configure compiler here: \Magento\Framework\View\TemplateEngine\Xhtml\Compiler and add item with name "comment" or with name "label". Extend this compiler from standard compiler that is used now. In this compiler you will have argument $processedObject. This argument should have information about readed section.

I hope this information will be helpfull for you.
@see
\Magento\Config\Model\Config\Structure\Reader
\Magento\Config\Model\Config\Compiler\IncludeElement
\Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface
\Magento\Framework\View\TemplateEngine\Xhtml\Compiler