Skip to content

Commit

Permalink
MAGETWO-84432: [Backport for 2.1 of #9904] #8069: Saving Category wit…
Browse files Browse the repository at this point in the history
…h existing imag… #12368
  • Loading branch information
Oleksii Korshenko authored Dec 12, 2017
2 parents 95eaf87 + 44f95e4 commit 14293ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
*/
private $imageUploader;

/**
* @var string
*/
private $additionalData = '_additional_data_';

/**
* Image constructor.
*
Expand Down Expand Up @@ -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, '');
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 14293ce

Please sign in to comment.