0

I have the following configuration field:

enter image description here

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?

2
  • Based on other configs field? Commented Jan 23, 2020 at 11:51
  • No, custom condition. In my case it is database table entities. Commented Jan 23, 2020 at 12:18

1 Answer 1

1

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

2
  • condition shouldn't be depend on another field, it should be depend on(in my case) custom database table -> if table is empty Commented Jan 23, 2020 at 12:17
  • I have updated please check Commented Jan 23, 2020 at 13:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.