{"id":537,"date":"2019-08-05T14:31:50","date_gmt":"2019-08-05T14:31:50","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/08\/05\/add-dynamically-generated-comment-for-backend-configuration-field-in-magento-2\/"},"modified":"2025-05-21T16:23:56","modified_gmt":"2025-05-21T10:53:56","slug":"add-dynamically-generated-comment-for-backend-configuration-field-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-dynamically-generated-comment-for-backend-configuration-field-in-magento-2\/","title":{"rendered":"How to Add Dynamically Generated Comment for a Backend Configuration Field in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As a Magento 2 developer, you may need to add comments in the backend configuration of extensions for the ease of admin or store owner. The extension holder might not be an expert in Magento 2 and hence such comments can guide them through the extension configuration and use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, in particular cases, the comments can have links to the store\u2019s URLs to show the output or redirect the admin to check the functionality implemented in the backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But, these links\u2019 paths differ from one Magento 2 store to another. The extension that you are going to develop is not necessarily going to be used for a single store only. Hence you need to&nbsp;<em><strong>add dynamically generated comment for a backend configuration field in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The post shows how to do so programmatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Dynamically Generated Comment for a Backend Configuration Field in Magento 2:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create&nbsp;<em><strong>system.xml<\/strong><\/em>&nbsp;at&nbsp;<strong><strong>app\\code\\Vendor\\Extension\\etc\\adminhtml<\/strong><\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;field id=\"dynamic_comment\" translate=\"label\" type=\"text\" sortOrder=\"22\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\">\n        &lt;label>Dynamic Comment&lt;\/label>\n        &lt;comment model=\"Vendor\\Extension\\Block\\Adminhtml\\System\\DynamicComment\" \/>\n&lt;\/field><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <strong><em>DynamicComment.php<\/em><\/strong> at <strong><strong>app\\code\\Vendor\\Extension\\Block\\Adminhtml\\System<\/strong><\/strong> folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Block\\Adminhtml\\System;\n\nuse Magento\\Framework\\View\\Element\\AbstractBlock;\nuse Magento\\Config\\Model\\Config\\CommentInterface;\n\nclass DynamicComment extends AbstractBlock implements CommentInterface\n{\n    public function getCommentText($elementValue)\n    {\n        $url = $this->_urlBuilder->getUrl('dynamic\/dynamic\/dynamic');\n        return \"This is a &lt;a href='$url'>Dynamically&lt;\/a> Generated Comment\";\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ve implemented the above method as shown here:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/08\/How-to-Add-Dynamically-Generated-Comment-for-a-Backend-Configuration-Field-in-Magento-2-Meetanshi.png\" alt=\"How to Add Dynamically Generated Comment for a Backend Configuration Field in Magento 2 - Meetanshi\" class=\"wp-image-6167\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Now you can fill your backend configuration with dynamic comments  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019d be very grateful if you helped share this helpful post on social media to fellow developers!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a Magento 2 developer, you may need to add comments in the backend configuration of extensions for the ease of admin or store owner&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-537","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=537"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/537\/revisions"}],"predecessor-version":[{"id":13778,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/537\/revisions\/13778"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}