0

I created a custom admin form using Ui component. In that form i need to show and hide a fieldset dynamically based on a condition. I tried to add class for that fieldset, But the class not working in Normal Fieldset or Non tab fieldset.

<fieldset name="menu_items" class="Vendor\Module\Ui\Component\Form\CustomFieldset">
        <settings>
            <collapsible>true</collapsible>
            <label>Menu Items</label>
            <visible>false</visible>
        </settings>
        <fieldset name="assign_applicants">
            <settings>
                <label>Menu Items</label>
                <collapsible>true</collapsible>
            </settings>

            <field name="name" sortOrder="30" formElement="input">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="source" xsi:type="string">menu</item>
                    </item>
                </argument>
                <settings>
                    <validation>
                        <rule name="required-entry" xsi:type="boolean">true</rule>
                    </validation>
                    <dataType>string</dataType>
                    <label translate="true">Name</label>
                    <dataScope>name</dataScope>
                </settings>
            </field>
        </fieldset>
    </fieldset>

Here I need to dynamically hide the menu_items fieldset. So I created a class as CustomFieldset

The CustomFieldset class is:

<?php
namespace Vendor\Module\Ui\Component\Form;

use Magento\Framework\View\Element\ComponentVisibilityInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Form\Fieldset;

class CustomFieldset extends Fieldset implements ComponentVisibilityInterface
{
    public function __construct(
        ContextInterface $context,
        array $components = [],
        array $data = []
    ) {
        $this->context = $context;

        parent::__construct($context, $components, $data);
    }

    //This method is responsible for show and hide the fieldset

    public function isComponentVisible(): bool
    {
        $visible = 0; // For now Hide the fieldset statically, later write the logics here
        return (bool)$visible;
    }
}

This class is working only the menu_items fieldset is on tab format. Whenever I tried to add class for (non tab) Normal fieldset, the class not working. So in this case how i dynamically hide the fieldset?

2 Answers 2

2

In your CustomFieldset class, overrides prepare function rather than isComponentVisible, the code should look like:

    public function prepare() {
        $visiable = // do some work to get boolean value;
        $config = $this->getData('config');
        $config['visible'] = $visiable;
        $this->setData('config', $config);
        parent::prepare();
    }

and then remove implements ComponentVisibilityInterface and the function isComponentVisible.

Hope it helps.

1

There are two way to hide the fieldset based on condition.

Step 1: Override prepare method of your fieldset like below.

Your Form.xml

<fieldset name="menu_items" class="Vendor\Module\Ui\Component\Form\CustomFieldset">
    <settings>
        <collapsible>true</collapsible>
        <label>Menu Items</label>
        <visible>false</visible>
    </settings>
    <fieldset name="assign_applicants">
        <settings>
            <label>Menu Items</label>
            <collapsible>true</collapsible>
        </settings>

        <field name="name" sortOrder="30" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">menu</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>string</dataType>
                <label translate="true">Name</label>
                <dataScope>name</dataScope>
            </settings>
        </field>
    </fieldset>
</fieldset>

Prepare method of your component fieldset class CustomFieldset.php

<?php

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace VendorName\ModuleName\Ui\Component\Form;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\ComponentVisibilityInterface;
use Magento\Ui\Component\Form\Fieldset;

/**
 * Class Fieldset
 * @package VendorName\ModuleName\Ui\Component\Form
 */
class CustomFieldset extends Fieldset
{
    /**
     * @var \VendorName\ModuleName\Helper\Data
     */
    protected $helper;

    /**
     * RuleInformationFieldset constructor.
     * @param ContextInterface $context
     * @param array $components
     * @param array $data
     * @param \VendorName\ModuleName\Helper\Data $helper
     */
    public function __construct(
        ContextInterface $context,
        array $components = [],
        array $data = [],
        \VendorName\ModuleName\Helper\Data $helper
    ) {
        parent::__construct($context, $components, $data);
        $this->helper = $helper;
    }

    /**
     * hide or show fieldset based on module enabled or disabled.
     */
    public function prepare()
    {
        $visiable = false;
        $config = $this->getData('config');

        if( $this->helper->isModuleEnable() ) {
            $visiable = true;
        }

        $config['visible'] = $visiable;
        $this->setData('config', $config);
        
        parent::prepare();
    }
}

Step 2: You need to disable the whole fieldset then add the following code in your Data provider class.

/**
 * Get meta function.
 * return mixed
 */
public function getMeta()
{
    $meta = parent::getMeta();

    if($this->helper->isModuleEnable()) {

        $meta['menu_items']['arguments']['data']['config']['visible'] = 1;
    } else{
        $meta['menu_items']['arguments']['data']['config']['visible'] = 0;

    }
    return $meta;
}

I hope it will help you or other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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