How to Add Link on Store Configuration Fields Comment in Magento 2

Whenever you are developing some extensions you need to add a few configurations with comments for user better understanding. However, in some cases, you want to add some comments with custom links to redirect the admin to another link for the best user experience. So, in this article, we are going to show you how to add custom links to backend configuration fields comment.

Before we start I assume, you have already a created custom module. If you don't have it or don't know how to create it then check out our other article How To Create a Magento 2 Module.

Let's get started !!

We hope you already have a system.xml file in your module. If you don't have it then check the below steps and create it.

Step 1: Create system.xml at app/code/Vendor/CustomValidation/etc/adminhtml and paste the below code. You need to change code as per your requirement.

saveCopyzoom_out_map
<?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> ... <field id="text" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Custom Text Field</label> <comment> <model>Vendor\CustomValidation\Model\Config\Comment</model> </comment> </field> ... </system> </config>

In the above code, you see we've used Vendor\CustomValidation\Model\Config\Comment to get a dynamic comment in the backend configuration. You'll use your model according to your requirement.

Now, let's create a model file to get a custom comment with links.

Step 2: Create Comment.php file at app/code/Vendor/CustomValidation/Model/Config/ to create module file. And paste the below code.

saveCopyzoom_out_map
<?php namespace Vendor\CustomValidation\Model\Config; use Magento\Framework\UrlInterface; class Comment implements \Magento\Config\Model\Config\CommentInterface { protected $urlInterface; public function __construct( UrlInterface $urlInterface ) { $this->urlInterface = $urlInterface; } public function getCommentText($elementValue) { $url = $this->urlInterface->getUrl('*/*/*/section/configdemo'); return 'This is custom <a href="' . $url . '"target="_blank">Link</a>.'; } }

Now, open Command line in folder root of magento and run the below commands.

saveCopyzoom_out_map
php bin/magento c:c

You will see link in comment like the below screenshot.

How to Add Link on Store Configuration Fields Comment in Magento 2

That’s it,

We hope this Magento post helped you to know How to Add Link on Store Configuration Fields Comment in Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off