Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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