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

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:

vendor/magento/module-catalog/Model/Category/Attribute/Backend/Image.php

vendor/magento/module-catalog/Controller/Adminhtml/Category/Image/Upload.php

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 the 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.

Check also:

Adding Image Attribute To a Category in Magento 2: Alternative Version



7 comments

  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.

  2. All id good, just need to add routes.xml and everything will working fine.
    LOVE U guys…

  3. It doesn’t upload any image file uploader is blank…
    @ankit which code you had written in routes.xml

  4. Thanks Mishel. This is very usefull
    For last method we have to create Helper\Data.php

    [ Vendor ]\[ Module ]\Helper\Data.php

    _storeManager = $storeManager;
    parent::__construct($context);
    }

    public function getCategoryThumbUrl($category) {
    $url = false;
    $image = $category->getImageThumb();
    if ($image) {
    if (is_string($image)) {
    $url = $this->_storeManager->getStore()->getBaseUrl(
    \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
    ) . ‘catalog/category/’ . $image;
    } else {
    throw new \Magento\Framework\Exception\LocalizedException(
    __(‘Something went wrong while getting the image url.’)
    );
    }
    }

    return $url;
    }

    }

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *