How To Create Input Tag With Disabled Attribute In Magento system.xml
If you are someone who develops Magento extensions for store admins or who deliver work to another developer, bookmarking this post might be of help!
The post gives the method to create input tag with disabled attribute in Magento system.xml.
For example, you are developing a module that requires an external API integration. You don’t want the user to change the API details from the backend even though he has the access to the admin panel and configuration of the extension.
In that case, you can simply disable the fields that you want not to be changed.
Other examples where you can implement this solution is when you don’t want the user to change the password, selected countries that are allowed to use a specific feature, etc.
Disable the attribute in Magento system.xml to get the result as shown below:
Method to Create Input Tag With Disabled Attribute in Magento system.xml:
Find if the field code in system.xml is as below:
1 2 3 4 5 6 7 8 |
<token translate="label"> <label>Auth Token</label> <frontend_type>text</frontend_type> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </token> |
and replace it with the below:
1 2 3 4 5 6 7 8 9 |
<token translate="label"> <label>Auth Token</label> <frontend_type>text</frontend_type> <frontend_model>Vendor_Extension_Block_Field_Disable</frontend_model> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </token> |
Create a new file Disable.php at Vendor\Extension\Block\Field folder
1 2 3 4 5 6 7 |
<?php class Vendor_Extension_Block_Field_Disable extends Mage_Adminhtml_Block_System_Config_Form_Field{ protected function _getElementHtml($element) { $element->setDisabled('disabled'); return parent::_getElementHtml($element); } } |
That’s it.
Secure your configuration with the above code.
Need further help on the topic? Please mention in the Comments section below. I’d be happy to help.
Do not forget to share the solution with fellow developers!
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Learn The Best Way to Pass Parameter to URL in Magento 2
How To Create Input Tag With Disabled Attribute In Magento 2 system.xml
Next