7

In my system.xml, i have a field tag as:

<field id="token" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Auth Token</label>
</field>

What is attribute i need add to field tag for disable input in Configuration ?

Thanks

19

I think <frontend_model> works for you as like Magento1

<field id="recipient_email" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Send Emails To</label>
                    <validate>validate-email</validate>
                    <frontend_model>YOUR_NAMESPACE\YOUR_MODULE\Block\System\Config\Form\Field\Disable</frontend_model>
                </field>

and your frontend_model class should be

<?php
namespace YOUR_NAMESPACE\YOUR_MODULE\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();

    }
}
9

Keyur Shah's answer is correct but I found that it is not possible to copy the value of the disabled element. So instead of writing $element->setDisabled('disabled'); you can write $element->setData('readonly', 1);. This displays the field the same way but you can mark the value and copy it.

2

This is not supported by current XSD of system.xml (Magento/Config/etc/system.xsd).

To enable such behavior for all types of fields at once it is possible to extend \Magento\Config\Block\System\Config\Form\Field::render() (e.g. using plugins) to make it support new type value of attribute element:

<field id="token" ...>
      <label>Auth Token</label>
      <attribute type="disabled">1</attribute>
</field>

At the moment it is possible to specify any value for type of attribute element, but only value shared will be processed, see \Magento\Config\Block\System\Config\Form::_getSharedCssClass(). Similar processing can be added in a new plugin to disable element if such attribute is specified.

0

another way to disable your field, is to set the value in the env.php file. The env.php allows you to set your configuration by default and disable the fields which are mapped in the env.php file. Your configuration could not be change by admin user. It's quite convenient if you want to put some sensitive settings.

'system' => [
   'default' => [
      'your_path => [
         'to_your' =>[
            'system_config =>['my special config in the store config']
          ],
       ],
   ]
]
-2

Try set all show* fields to 0

<field id="token" showInDefault="0" showInWebsite="0" showInStore="0">
</field>
  • I want input tag disable, but if set attribute as you, it will hide – Thao Pham Nov 19 '15 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.