How to hide attributes in admin product edit view in Magento 2
Sometimes store owners value simplicity and by default the product edit form is anything but that. If you couple that with the fact that usually stores have dozens of custom attributes and sections for 3rd party vendors we are looking at a very large form. Magento attempts to deal with this using collapsible sections however even so the view is too complex for most users.
In this article we will explore how we can hide attributes and sections of attributes from admin product view. This can allow us to simplify the view by removing the sections we do not need and making it easier for store owners to do their work. Lets get into it.
How attribute form is built
Before we start lets see how the product edit view is implement. The part we are interested is the attribute form. If we look into the source we can see that the form is implemented as a ui component.
The ui component is defined in Catalog module. The section we are interested is how the data is being passed into the component. Because we want to modify the form to hide certain fields.
The important bit here is the data provider declaration. As we can see that we have a specific class that provides the data.
The following function is responsible for processing the field meta data and passing it to the UI component. We could hook right in here with a plugin and modify the data that is returned. However in this example we will go one level deeper and figure out how data is collected for each field and modify it there.
Ok, so now we now that the meta data is collected here. Next lets see how the modifier instances are declared. For this we need to take a look into the di.xml file.
First we can see that the pool argument that was used in the data provider is replaced with a new class Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool. If you will notice this is a virtual class that is only used in di.xml to reference the pool. You can read more about types and virtual types in our previous article How to use type and virtual type declarations.
Here we can see that the modifiers are passed in as arguments for the newly defined virtual class. Each modifier defines its own implementation of data provider.
Example for hiding an attribute section
Next we will show you how to modify the data provider to hide a section without rewriting native code.
Declare plugin
We will create a plugin in our module. This will allow us to hook into the data provider class and modify the meta data for the form component.
Create our plugin class.
As you can see we are using and after plugin which allows us to modify the result of a method. We are hooking into modifyMeta method which allows us to change the component configuration as an array.
The important part here is the
$result['custom_options']['arguments']['data']['config']['visible'] = false;
We are defining a new property visible and setting it to false. This will ensure that the Customizable Options section is hidden.
In a similar manner we could disable and attribute or a section. Instead of using the visible property we could set a disabled property. The end result would look similar to below.
Hopefully this gives you some ideas on how to modify the attribute sections and a better understanding on how the UI components are utilized in Magento 2.