How to Add Dynamically Generated  Link in Comment Field in System Configuration in Magento 2

Hello Everyone,

In this blog, we will learn about how to Add Dynamically Generated Link in Comment Field in system.xml Configuration in Magento 2.

Adding a link in the comment field is useful when you want to provide some additional information.

This is useful when you have to give external reference, documentation etc. related to the field.

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add link in Comment Field in Configuration file in Magento 2.

STEPS FOR ADD LINK IN COMMENT FIELD IN SYSTEM CONFIGURATION IN MAGENTO 2

Step 1: Create system.xml file

app/code/Vendor/Extension/etc/adminhtml/system.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">

    <system>

        <tab id="magecurious" class="magecurious" translate="label" sortOrder="10">

            <label>Magecurious</label>

        </tab>

        <section id="extension" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">

            <class>separator-top</class>

            <label>Magecurious</label>

            <tab>magecurious</tab>

            <resource>Vendor_Extension::config_extension</resource>

            <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">

                <label>General Configuration</label>

                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">

                    <label>Enable Extension</label>

                 <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>

                </field>

<field id="vendor_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">

                    <label>Custom Field</label>

                    <frontend_model>Vendor\Module\Block\Adminhtml\System\Config\Link</frontend_model>

                </field>

</group>

        </section>

    </system>

</config>

Step 2: Create Link.php file

app/code/Vendor/Extension/Block/Adminhtml/System/Config/Link.php

<?php

namespace Vendor\Module\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;

use Magento\Framework\Data\Form\Element\AbstractElement;

class Link extends Field

{

    /**

     * Render the element and add a custom link to the comment field.

     *

     * @param AbstractElement $element

     * @return string

     */

    protected function _getElementHtml(AbstractElement $element)

    {

        $element->setComment(

            'This is a custom field. Click <a href="https://www.magecurious.com" target="_blank">here</a> for more information.'

        );

        return parent::_getElementHtml($element);

    }

}

Step 3: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 4: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Dynamically Generated Link in Comment Field in system.xml configuration file in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *