cancel
Showing results for 
Search instead for 
Did you mean: 

Disabling a UI component field while add new product in Magento 2

Disabling a UI component field while add new product in Magento 2

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.

If Issue Solved, Click Kudos/Accept As solutions.
3 REPLIES 3

Re: Disabling a UI component field while add new product in Magento 2

Hello @Patel_Chirag

 

you need to create the plugin for that into adminhtml folde

 


<preference for="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AttributeSet" type="CompanyName\Modulename\Ui\DataProvider\Product\Form\Modifier\AttributeSet" />

 

In that AttributeSet.php

<?php
namespace CompanyName\ModuelName\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
use Magento\Framework\UrlInterface;

/**
 * Add "Attribute Set" to first fieldset
 */
class AttributeSet extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AttributeSet
{
    protected $_dataHelper;
    /**
     * @param LocatorInterface $locator
     * @param CollectionFactory $attributeSetCollectionFactory
     * @param UrlInterface $urlBuilder
     */
    public function __construct(
        LocatorInterface $locator,
        CollectionFactory $attributeSetCollectionFactory,
		UrlInterface $urlBuilder,
    ) {
	parent::__construct($locator,$attributeSetCollectionFactory,$urlBuilder);
    }

    public function modifyMeta(array $meta)
    { 
       $meta = parent::modifyMeta($meta);
     unset($meta['product-details']['children']['container_product_supplier_select']);   //here you need to find own meta and unset that, before that you need to get product id from url.
     }

Hope it will help you.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Disabling a UI component field while add new product in Magento 2

Hello @Patel_Chirag

 

are you facing any issue still?


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Disabling a UI component field while add new product in Magento 2

Hello @Patel_Chirag ,

 

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']['children']['field_name']['arguments']['data']['config']['visible'] = 1;
}
else{
$meta['fieldset_name']['children']['field_name']['arguments']['data']['config']['visible'] = 0; } return $meta; }

Problem solved? Click Kudos & Accept as Solution!


This worked for me.