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?

  • Based on other configs field? – Dhaduk Mitesh Jan 23 at 11:51
  • No, custom condition. In my case it is database table entities. – dukir Jan 23 at 12:18
0

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

|improve this answer|||||
  • condition shouldn't be depend on another field, it should be depend on(in my case) custom database table -> if table is empty – dukir Jan 23 at 12:17
  • I have updated please check – Ranganathan Jan 23 at 13:19

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.