5

I have created custom image attribute. This is displaying perfect. But when i save category, it is not saving. Images are uploading perfectly, Saving at /media/tmp, but not saving in media/catalog after saving category magento 2.

2
  • When you upload image, it showing preview before save ? Oct 7, 2016 at 7:40
  • Did you ever solve this? I just ran into this problem with a custom module. Jul 17, 2017 at 10:45

3 Answers 3

1

Magento2.1 Category image upload showing preview after upload image but not working after save category.

this is magento version default bug.

To solve this issue by apply bellow patch.

Opne this url : https://github.com/magento/magento2/pull/5978/files

You have to override below core files code with file code from given URL.

vendor/magento/module-catalog/Controller/Adminhtml/Category/Image/Upload.php
vendor/magento/module-catalog/Controller/Adminhtml/Category/Save.php
vendor/magento/module-catalog/Model/Category.php
vendor/magento/module-catalog/Model/Category/Attribute/Backend/Image.php
vendor/magento/module-catalog/Model/Category/DataProvider.php

Now magento-2.1 working code for custom image attribute of category.

See complete answer for the issue : See my complete answer

Let me know if you have any query/concern from above.

2
  • This is still not working Oct 7, 2016 at 12:26
  • You can get reference from given my answer otherwise you have add your code so i can debug on that. Oct 7, 2016 at 13:04
0

I solved this issue by setting the backend_model for my attribute in the eav_attribute table to Magento\Catalog\Model\Category\Attribute\Backend\Image

When a category is saved in Magento 2 it walks through the attributes calling the afterSave function of each attribute.

As you can see below the function Magento\Catalog\Model\Category\Attribute\Backend\Image::afterSave moves the file from the tmp folder to correct folder.

public function afterSave($object)
{
    $image = $object->getData($this->getAttribute()->getName(), null);

    if ($image !== null) {
        try {
            $this->getImageUploader()->moveFileFromTmp($image);
        } catch (\Exception $e) {
            $this->_logger->critical($e);
        }
    }
    return $this;
}
1
  • this fails in \Magento\Catalog\Model\ImageUploader::moveFileFromTmp as the $baseTmpImagePath contains both $basePath and $baseTmpPath => the copyFile method fails
    – Radu
    Apr 14, 2020 at 8:53
0

Check if you have created a preference for Magento\Catalog\Model\Category\DataProvider, and your new DataProvider has this code in it.

class DataProvider extends \Magento\Catalog\Model\Category\DataProvider {

protected function getFieldsMap()
{
    $fields = parent::getFieldsMap();
    $fields['content'][] = 'custom_attribute_name';
    return $fields;
}
2
  • can i override for invoice abstactpdf.php file ?
    – Rv Singh
    Oct 26, 2018 at 6:31
  • You can override only the public methods of AbstractPdf.php For protected methods, you can override the methods from where these methods are called. For example to override the method called insertLogo, override the Invoice.php which is calling insertLogo in getPdf method. Oct 29, 2018 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.