4

I have a UI component defining field, I want to display this field while edit existing product, and hide this field when add new product.

Namespace\Modulename\view\adminhtml\ui_component\product_form

<fieldset name="vendorproduct">  
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Seller Sell This Product</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">65</item>
        </item>
    </argument>
    <container name="vendor_data" >
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
        <htmlContent name="html_content">
            <argument name="block" xsi:type="object">Namespace\Modulename\Block\Adminhtml\Catalog\Product\Edit\Tab\Seller</argument>
        </htmlContent>
    </container>
</fieldset>

Using <item name="disabled" xsi:type="boolean">true</item> I can disable fields but I want to disable them while adding new product only. how can I achieve this.

Any help is appreciated. Thanks.

2
 <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="name" xsi:type="array">
                <item name="class" xsi:type="string">Namespace\ModuleNam\Ui\DataProvider\Product\Form\Modifier\Attributes</item>
                <item name="sortOrder" xsi:type="number">1000</item>
            </item>

        </argument>
    </arguments> 
</virtualType>


public function modifyMeta(array $meta)
{
    // check to disable attributes on product new
    if ($this->lumaData->getGeneralConfig('lockattributes') == 1) {
        // attributes need to be disabled
        $attributes = array();
    } else {

    $attributes = array();
    }
    if ($this->httpRequest->getActionName() == 'new' ) {                       
        // code to make the attribute disable
        foreach($attributes as $attribute){

            $path = $this->arrayManager->findPath($attribute, $meta, null, 'children');
            if ($path) {
                $meta = $this->arrayManager->set(
                 "{$path}/arguments/data/config/disabled",
                 $meta,
                 true
                );   
            }
        }
    }
    return $meta;
}
If you use this you will get solution
1

According to the official docs Modifier classes should be used when you need to add validations to display only certain fields for certain product types and also when static declaration is not applicable. Which I think is the case you are describing here.

https://devdocs.magento.com/guides/v2.3/howdoi/customize-modifier-class.html

1

You can override getMeta() function in your DataProvider class

public function getMeta()
    {
        $meta = parent::getMeta();
        $id = $this->request->getParam('entity_id');
        if(isset($id)){

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

        }
        return $meta;
    }

This worked for me.

0

The correct way to disable is adding the following method to the DataProvider class:

public function getMeta() : array
{
    $meta = parent::getMeta();

    // check if Some field should be editable
    $isEnabled = ($this->request->getParam('id') == NULL) ? false : true;
    $meta['fieldset_name']['children']['field_name']['arguments']['data']['config']['disabled'] = $isEnabled;

    return $meta;
}

Please get sure the parameter "ID" is the primary key of your table. Also, note you must replace "fieldset_name" and "field_name" with your data.

If you want the field to not show at all, use "visible" instead of "disabled".

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.