Logo

How to add a multiselect EAV attribute on Magento 2 Cateogory

This will give you an overview about how to add an EAV multiselect attribute on category entity on backend.

Context:

I am creating this tutorial while I develop a feature for one the Magento 2 projects I am currently working on, called Product Tags. This feature allows users to filter products by tags on frontend at category level.
For this we need an attribute which will store all the product tags for the entire category. In this tutorial I am describing how to add this EAV attribute to the category.

Steps:

We will go trough the following steps via a new module called here Bb_Tags:

  1. Creating and registering the new module ( we won’t cover this ).
  2. Creating the EAV attribute by Setup\InstallData script.
  3. Add the form field via adminhtml/view/category_form.xml file.
  4. Creating the EAV attribute BackendModel.
  5. Testing the new Module by having the values stored for one category.

 

2. Creating the EAV attribute by Setup\InstallData script

In order to accomplish this step, we only need to create a new install script on the new module.

This will be executed only once when the cli command setup:upgrade  will be executed first time after the module:enable Bb_Tags  .

Notes:
a) Here we need to have an \Magento\Eav\Setup\EavSetup  object injected on the __construct() method. Optionally we included additional objects and factories in order to add some mock options, you can skip using these.
b) We need to use this object to create a catalog_category EAV attribute.
c) Source Model for the EAV attribute is: Magento\Eav\Model\Entity\Attribute\Source\Table
d) Custom Backend Model is required here in order to have the values imploded by a comma at save.
Here is the  Bb\Tags\Setup\InstallData.php  file:

 

3. Add the form field via adminhtml/view/category_form.xml file

As we can see in the Magento_Catalog module, the category form fields are specified in the vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml file.

Similarly we will have our own Bb/Tags/view/adminhtml/ui_component/category_form.xml file that is handling the displaying of our multiselect filed:

Notes:
a) This field is displayed in the content accordion tab that already exists.

 

After module:enable  and setup:upgrade  cli commands, you should be able to see this:

 

Capture

If you hit save, you’ll see that no actual values are stored for ‘Category Tags’ field. And that’s because we need to modify the options format.
[Offtopic] I didn’t figured out why the EAV model cannot accept array for multiselect values when saving the options for a certain entity.
If you are willing to dig more, check vendor/magento/module-eav/Model/ResourceModel/UpdateHandler.php at line 129. If you won’t implement a custom Backend Model that simply implodes the values into a comma sep. string, it won’t pass these lines.

 

4. Creating the EAV attribute BackendModel

On this step we will create the custom backendModel we used at previous step on attribute creation. By having this we will accomplish the actual storing tags attribute options for category procedure.

So let’s go and create the Bb/Tags/Model/Category/Attribute/Backend/Tags.php file:

You’ll be able to save now, and see that the selected values persists.

Let me know if you succeed!

 

 

Back to blog


Leave A Comment