Skip to content

Commit 9190008

Browse files
Merge pull request #1839 from magento-engcom/2.1-develop-prs
[EngCom] Public Pull Requests - 2.1-develop - MAGETWO-84432: Saving Category with existing image causes an exception #12368
2 parents 477f536 + ff7e370 commit 9190008

File tree

2 files changed

+25
-7
lines changed
  • app/code/Magento/Catalog

2 files changed

+25
-7
lines changed

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
5252
*/
5353
private $imageUploader;
5454

55+
/**
56+
* @var string
57+
*/
58+
private $additionalData = '_additional_data_';
59+
5560
/**
5661
* Image constructor.
5762
*
@@ -80,9 +85,9 @@ public function beforeSave($object)
8085
{
8186
$attributeName = $this->getAttribute()->getName();
8287
$value = $object->getData($attributeName);
83-
$imageName = $this->getUploadedImageName($value);
8488

85-
if ($imageName) {
89+
if ($imageName = $this->getUploadedImageName($value)) {
90+
$object->setData($this->additionalData . $attributeName, $value);
8691
$object->setData($attributeName, $imageName);
8792
} else if (!is_string($value)) {
8893
$object->setData($attributeName, '');
@@ -125,15 +130,27 @@ private function getImageUploader()
125130
}
126131

127132
/**
128-
* Save uploaded file and set its name to category.
133+
* Check if temporary file is available for new image upload.
134+
*
135+
* @param array $value
136+
* @return bool
137+
*/
138+
private function isTmpFileAvailable($value)
139+
{
140+
return is_array($value) && isset($value[0]['tmp_name']);
141+
}
142+
143+
/**
144+
* Save uploaded file and set its name to category
129145
*
130146
* @param \Magento\Framework\DataObject $object
131147
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
132148
*/
133149
public function afterSave($object)
134150
{
135-
$imageName = $object->getData($this->getAttribute()->getName(), null);
136-
if ($imageName) {
151+
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());
152+
153+
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
137154
try {
138155
$this->getImageUploader()->moveFileFromTmp($imageName);
139156
} catch (\Exception $e) {

app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public function testAfterSave()
164164

165165
$object = new \Magento\Framework\DataObject(
166166
[
167-
'test_attribute' => 'test1234.jpg'
167+
'test_attribute' => 'test1234.jpg',
168+
'_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']]
168169
]
169170
);
170171
$model->afterSave($object);
@@ -207,7 +208,7 @@ public function testAfterSaveWithExceptions()
207208
->with($this->equalTo($exception));
208209
$object = new \Magento\Framework\DataObject(
209210
[
210-
'test_attribute' => 'test1234.jpg'
211+
'_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']]
211212
]
212213
);
213214
$model->afterSave($object);

0 commit comments

Comments
 (0)