diff --git a/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php b/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php index a8bc510817f1b..2a29f8d62ca55 100644 --- a/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php +++ b/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php @@ -52,6 +52,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend */ private $imageUploader; + /** + * @var string + */ + private $additionalData = '_additional_data_'; + /** * Image constructor. * @@ -80,9 +85,9 @@ public function beforeSave($object) { $attributeName = $this->getAttribute()->getName(); $value = $object->getData($attributeName); - $imageName = $this->getUploadedImageName($value); - if ($imageName) { + if ($imageName = $this->getUploadedImageName($value)) { + $object->setData($this->additionalData . $attributeName, $value); $object->setData($attributeName, $imageName); } else if (!is_string($value)) { $object->setData($attributeName, ''); @@ -125,15 +130,27 @@ private function getImageUploader() } /** - * Save uploaded file and set its name to category. + * Check if temporary file is available for new image upload. + * + * @param array $value + * @return bool + */ + private function isTmpFileAvailable($value) + { + return is_array($value) && isset($value[0]['tmp_name']); + } + + /** + * Save uploaded file and set its name to category * * @param \Magento\Framework\DataObject $object * @return \Magento\Catalog\Model\Category\Attribute\Backend\Image */ public function afterSave($object) { - $imageName = $object->getData($this->getAttribute()->getName(), null); - if ($imageName) { + $value = $object->getData($this->additionalData . $this->getAttribute()->getName()); + + if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) { try { $this->getImageUploader()->moveFileFromTmp($imageName); } catch (\Exception $e) { diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php index 9ad566cf93e36..96ee74254763f 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php @@ -164,7 +164,8 @@ public function testAfterSave() $object = new \Magento\Framework\DataObject( [ - 'test_attribute' => 'test1234.jpg' + 'test_attribute' => 'test1234.jpg', + '_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']] ] ); $model->afterSave($object); @@ -207,7 +208,7 @@ public function testAfterSaveWithExceptions() ->with($this->equalTo($exception)); $object = new \Magento\Framework\DataObject( [ - 'test_attribute' => 'test1234.jpg' + '_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']] ] ); $model->afterSave($object);