I have the following configuration field:
I'm using a backend_model and frontend_model for rendering this field with two options.
How can I disable it with a condition? What should I use?
I have the following configuration field:
I'm using a backend_model and frontend_model for rendering this field with two options.
How can I disable it with a condition? What should I use?
You can acheive by using frontend model. try below step
system.xml
<field id="your_field_id" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Cargo Type Mapping</label> <frontend_model>Vendor\Module\Block\Adminhtml\CargoFrontend</frontend_model> <backend_model>Vendor\Module\Model\Config\Source\CargoBackend</backend_model> </field> Vendor\Module\Block\Adminhtml\CargoFrontend.php
<?php namespace Vendor\Module\Block\Adminhtml; class CargoFrontend extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray { public function __construct( \Magento\Backend\Block\Template\Context $context, array $data = [] ) { parent::__construct($context, $data); } /** * {@inheritdoc} */ protected function _prepareToRender() { $this->addColumn('tab1',['label' => __('Cargo Type'), 'class' => 'required-entry']); $this->addColumn('tab2',['label' => __('Attribute Set'), 'class' => 'required-entry']); $this->_addAfter = false; $this->_addButtonLabel = __('Add'); } /** * Retrieve HTML markup for given form element * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string */ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $isCheckboxRequired = $this->_isInheritCheckboxRequired($element); // Disable element if value is inherited from other scope. Flag has to be set before the value is rendered. if ($element->getInherit() == 1 && $isCheckboxRequired) { $element->setDisabled(true); } $html = '<td class="label"><label for="' . $element->getHtmlId() . '"><span' . $this->_renderScopeLabel($element) . '>' . $element->getLabel() . '</span></label></td>'; $html .= $this->_renderValue($element); if ($isCheckboxRequired) { $html .= $this->_renderInheritCheckbox($element); } $html .= $this->_renderHint($element); return $this->_decorateRowHtml($element, $html); } /** * Decorate field row html * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @param string $html * @return string */ protected function _decorateRowHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element, $html) { if(1 == 2){//you can add your condition based on table entities $style = 'style="display: none"'; } else { $style = 'style="display: block"'; } return '<tr id="row_' . $element->getHtmlId() . $style .'">' . $html . '</tr>'; } } you can add your condition in _decorateRowHtml function from frontend model