0

I have created custom field in Magento 2.4.1 product edit page,

product_form.xml

<field name="type" formElement="select">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Ceymox\DigitalCard\Model\DigitalCard\Options</item>
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">block</item>
                </item>
            </argument>
            <settings>
                <dataType>select</dataType>
                <label translate="true">Digital Card Type</label>
                <dataScope>type</dataScope>
            </settings>
        </field>

        <file name="csv_uploader" >
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">CSV File</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="formElement" xsi:type="string">fileUploader</item>
                    <item name="componentType" xsi:type="string">fileUploader</item> 
                    <item name="notice" xsi:type="string" translate="true">Allowed file type: csv.</item>
                    <item name="allowedExtensions" xsi:type="string">csv</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="digitalcard/index/generate"/>
                    </item> 
                </item>
            </argument>
        </file>
        
        <insertListing name="digitalcard_listing" >
            <settings>
                <dataLinks>
                    <exports>false</exports>
                    <imports>true</imports>
                </dataLinks>
                <autoRender>true</autoRender>
                <selectionsProvider>digitalcard_listing.digitalcard_listing.columns.ids</selectionsProvider>
                <dataScope>digitalcard_listing</dataScope>
                <ns>digitalcard_listing</ns>
            </settings>
        </insertListing>
     </fieldset>

Now I need to hide this field based on condition(based on product type).How can i do this? can anyone help me on this

2 Answers 2

0

Check this link for reference :-

How to Create Dependent Fields in Forms using Ui components- Magento2

1
  • This toggles visibility based on the value of another form element. The question is about using the product type to determine visibility.
    – snez
    May 30 at 8:37
0

Magento combines the data from your product_form.xml file with any custom data in you modifier class. What the Magento docs are not very clear about is the merging order. Magento uses whatever your modifyMeta() method returns to override the config from product_form.xml. This means that you can do the following:

public function __construct(
    \Magento\Catalog\Model\Locator\LocatorInterface $locator
)
{
    $this->locator = $locator;
}

public function modifyMeta(array $meta): array
{
    $product = $this->locator->getProduct();

    if ($product->getTypeId() != "configurable")
    {
        $meta['your-group-id']['children']['your_field_id']['arguments']['data']['config'] = [
            "componentType" => "fieldset",
            "collapsible" => false,
            "sortOrder" => 1,
            'opened' => false,
            'canShow' => false,
            'visible' => false
        ];
    }

    return $meta;
}

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.