How to create a input tag have disable attribute in system.xml Magento 2
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();
}
}
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.
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.