Magento 2 Disable Input Field of system.xml file

While working on a Magento ecommerce website, you may encounter a situation where you need to disable certain input fields in the system configuration within the Magento 2 admin panel.

This is important to prevent admin users from altering the values of these fields, especially when the values are meant to remain static. Accidental changes to these values can impact the website’s logic, so it’s best to keep these fields read-only in the Magento admin.

Understanding system.xml

The system.xml file is an admin configuration file that enables you to define configuration settings for your module. It is usually located in the etc/adminhtml directory of your module. Each field in the system.xml file represents a setting that can be controlled from the Magento Admin Panel.

How to disable the system configuration input field programmatically in the Magento 2

Below are steps to make system configuration fields read only from the Magento admin.

Step 1: Find the system.xml file containing the fields you want to make read-only.

Step 2: Identify the input field you want to disable in the system.xml file.

Step 3: Add a new child tag frontend_model to the existing field as shown below.

<field id="gateway_url" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Gateway URL</label>
      <frontend_model>Dhairvi\ExtensionDemo\Block\Adminhmlt\System\Config\Disable</frontend_model>
</field>

Step 4: Create a file based on the location specified in <frontend_model> tag in the above step.

Here, we are creating the file Disable.php under \app\code\Dhairvi\ExtensionDemo\Block\Adminhtml\System\Config\

<?php
namespace Dhairvi\ExtensionDemo\Block\Adminhtml\System\Config

use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Config\Block\System\Config\Form\Field;

class Disable extends Field
{
    protected function _getElementHtml(AbstractElement $element)
    {
        $element->setDisabled('disabled');
        return $element->getElementHtml();
    }
}

Clear Magento caches, and in output, you can see that the filed is now disabled, and the admin can’t update that filed value from the admin.

Read only fields in Magento 2 admin



There is one more way to make system configuration values read-only by adding those values to /app/etc/env.php or /app/etc/config.php.

Check out our blog post “Magento 2 system.xml Field Dependency on Multiple Values” to learn how to set the system configuration field dependency on multiple values in Magento 2.

If you have any questions or need further assistance with Magento 2 development, feel free to get back to us.

Privacy Policy © 2024-2026 Dhairvi Solutions LLP. All Rights Reserved.