How to Add “Image” Attribute to a Category in Magento 2

December 20, 2016 / Posted in Magento, Magento 2 by Mishel Soiko
How to Add “Image” Attribute to a Category in Magento 2

It’s much easier to add “Image” attribute during upgrade or installation process of your module in the following files:

app/code/[ Vendor ]/[ Module ]/Setup/InstallData.php

Or here:

app/code/[ Vendor ]/[ Module ]/Setup/UpgradeData.php

Let me show you how to perform it during upgrade:

The second step you have to do is to add this attribute to a category form:

app/code/[ Vendor ]/[ Module ]/view/adminhtml/ui_component/category_form.xml

It would be enough for regular attributes, but not in case of the uploaded “image” attribute. Let’s find out what else we should do to add this attribute properly.

We have already defined the backend model:

And we have defined another URL for the upload:

Probably you are wondering, why do we need another Controller and a new Model? Why can’t we use the default controller and model, that is already defined in the “Image” attribute:



The answer is pretty simple: Magento 2.1.2 still has some versatility issues and code of attribute is placed right in the files. But we have our own attribute, that’s why we should create a new one:

app/code/[ Vendor ]/[ Module ]/Controller/Adminhtml/Category/Thumb/Upload.php

As you can see, the most important thing here is to specify the field in which the uploaded file $this->imageUploader->saveFileToTmpDir(‘image_thumb’) should be searched.

This Controller uploads an image to a temporary directory with the AJAX request. And shows its thumbnail in thr admin panel even before the category has been saved.

Further we need to define a model, which allows us to save the uploaded file, but now during the category saving process.

app/code/[ Vendor ]/[ Module ]/Model/Category/Attribute/Backend/Thumb.php

After the AJAX request, imageUploader returns not only an image itself, but an array with the updated data for the category object. And we should write another string to add the attribute to varchar.

beforeSave – transforms received array to a file name.

afterSave – transfers a file from temporary category to a constant one.

But there is more to come. We haven’t defined those file types, that could be uploaded. And those categories in which should use the uploaded image.

However we already specified the backend model for these configurations. They are defined in the
app/code/[ Vendor ]/[ Module ]/etc/di.xml

It seems that we finally finished, but after image uploading and category saving admin panel is getting stucked.

Additionally we should define the rewrite for DataProvider file in the di.xml: app/code/[ Vendor ]/[ Module ]/Model/Category/DataProvider.php

And in the end we have to display this image on the frontend. There is only a file name in the category data. And the following function will help to create a full URL for it.


  1. Thanks Mishel. A very useful tutorial.
    It’s actually something I’ve stuck in for a week.
    But I can’t get your code to work either. Would you kindly add a ready-to-use zip of your module code to your article.

Post a new comment