Hello Magento Friends,
Hope all are doing great! Today’s learning topic is How To Disable System Configuration Field Programmatically in Magento 2. Check out the previously published blog, How to Change Length of Order Increment ID in Magento 2.
When there are multiple admins in Magento 2, there are some system configuration fields like login credentials or API credentials. These fields are common for all admins and the master admin wants to prevent editing of these fields by others. The master admin creates these fields and does not want anyone to change the system configuration fields. To do this, the admin can disable system configuration fields programmatically in Magento 2.
Steps to Disable System Configuration Field Programmatically in Magento 2:
Step 1: Create Disable.php at the given below path
app\code\Vendor\Extension\Block\System\Config\Form\Field\Disable.php
Add the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace Vendor\Extension\Block\System\Config\Form\Field; use Magento\Framework\Data\Form\Element\AbstractElement; class Disable extends \Magento\Config\Block\System\Config\Form\Field { protected function _getElementHtml(AbstractElement $element) { $element->setDisabled('disabled'); return $element->getElementHtml(); } } |
Step 2: Add the below line in your system.xml on the specific field which you want to disable.
1 |
<frontend_model>Vendor\Extension\Block\System\Config\Form\Field\Disable</frontend_model> |
Output: Once you implement the above steps, that particular field is disabled in the Magento 2 system configuration.
Conclusion:
Accordingly, you can Disable System Configuration Field Programmatically in Magento 2. If you have any doubts, ping me via the comment box. I will be happy to solve it for you. Make sure you share the solution with your friends and colleagues. Stay connected with us!
Happy Coding!