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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago