How To

How To Disable System Configuration Field Programmatically in Magento 2

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

<?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.

<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!

Click to rate this post!
[Total: 7 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago