Skip to content

Commit 70d3468

Browse files
author
Allan Paiste
committed
change category image attribute implementation to work with any attribute code rather than working only with 'image'
1 parent f2d309a commit 70d3468

File tree

6 files changed

+115
-63
lines changed

6 files changed

+115
-63
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Image/Upload.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ protected function _isAllowed()
5050
*/
5151
public function execute()
5252
{
53+
$imageId = $this->_request->getParam('param_name', 'image');
54+
5355
try {
54-
$result = $this->imageUploader->saveFileToTmpDir('image');
56+
$result = $this->imageUploader->saveFileToTmpDir($imageId);
5557

5658
$result['cookie'] = [
5759
'name' => $this->_getSession()->getName(),

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

+28-33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Controller\Adminhtml\Category;
77

88
use Magento\Store\Model\StoreManagerInterface;
9+
use \Magento\Catalog\Api\Data\CategoryAttributeInterface;
910

1011
/**
1112
* Class Save
@@ -48,6 +49,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
4849
*/
4950
private $storeManager;
5051

52+
/**
53+
* @var \Magento\Eav\Model\Config
54+
*/
55+
private $eavConfig;
56+
5157
/**
5258
* Constructor
5359
*
@@ -56,43 +62,22 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
5662
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
5763
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
5864
* @param StoreManagerInterface $storeManager
65+
* @param \Magento\Eav\Model\Config $eavConfig
5966
*/
6067
public function __construct(
6168
\Magento\Backend\App\Action\Context $context,
6269
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
6370
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
6471
\Magento\Framework\View\LayoutFactory $layoutFactory,
65-
StoreManagerInterface $storeManager
72+
StoreManagerInterface $storeManager,
73+
\Magento\Eav\Model\Config $eavConfig
6674
) {
6775
parent::__construct($context);
6876
$this->resultRawFactory = $resultRawFactory;
6977
$this->resultJsonFactory = $resultJsonFactory;
7078
$this->layoutFactory = $layoutFactory;
7179
$this->storeManager = $storeManager;
72-
}
73-
74-
/**
75-
* Filter category data
76-
*
77-
* @param array $rawData
78-
* @return array
79-
*/
80-
protected function _filterCategoryPostData(array $rawData)
81-
{
82-
$data = $rawData;
83-
// @todo It is a workaround to prevent saving this data in category model and it has to be refactored in future
84-
if (isset($data['image']) && is_array($data['image'])) {
85-
if (!empty($data['image']['delete'])) {
86-
$data['image'] = null;
87-
} else {
88-
if (isset($data['image'][0]['name']) && isset($data['image'][0]['tmp_name'])) {
89-
$data['image'] = $data['image'][0]['name'];
90-
} else {
91-
unset($data['image']);
92-
}
93-
}
94-
}
95-
return $data;
80+
$this->eavConfig = $eavConfig;
9681
}
9782

9883
/**
@@ -126,7 +111,7 @@ public function execute()
126111
$this->storeManager->setCurrentStore($store->getCode());
127112
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
128113
if ($categoryPostData) {
129-
$category->addData($this->_filterCategoryPostData($categoryPostData));
114+
$category->addData($categoryPostData);
130115
if ($isNewCategory) {
131116
$parentCategory = $this->getParentCategory($parentId, $storeId);
132117
$category->setPath($parentCategory->getPath());
@@ -248,18 +233,28 @@ public function execute()
248233
}
249234

250235
/**
251-
* Image data preprocessing
252-
*
253-
* @param array $data
254-
*
236+
* @param $data
255237
* @return array
256238
*/
257239
public function imagePreprocessing($data)
258240
{
259-
if (empty($data['image'])) {
260-
unset($data['image']);
261-
$data['image']['delete'] = true;
241+
$entityType = $this->eavConfig->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);
242+
243+
foreach ($entityType->getAttributeCollection() as $attributeModel) {
244+
$attributeCode = $attributeModel->getAttributeCode();
245+
$backendModel = $attributeModel->getBackend();
246+
247+
if (isset($data[$attributeCode])) {
248+
continue;
249+
}
250+
251+
if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
252+
continue;
253+
}
254+
255+
$data[$attributeCode] = false;
262256
}
257+
263258
return $data;
264259
}
265260

app/code/Magento/Catalog/Model/Category.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,14 @@ public function formatUrlKey($str)
652652
}
653653

654654
/**
655-
* Retrieve image URL
656-
*
657-
* @return string
655+
* @param string $attributeCode
656+
* @return bool|string
657+
* @throws \Magento\Framework\Exception\LocalizedException
658658
*/
659-
public function getImageUrl()
659+
public function getImageUrl($attributeCode = 'image')
660660
{
661661
$url = false;
662-
$image = $this->getImage();
662+
$image = $this->getData($attributeCode);
663663
if ($image) {
664664
if (is_string($image)) {
665665
$url = $this->_storeManager->getStore()->getBaseUrl(

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

+57-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
1515
{
16+
const ADDITIONAL_DATA_SUFFIX = '_additional_data';
17+
1618
/**
1719
* @var \Magento\MediaStorage\Model\File\UploaderFactory
1820
*
@@ -21,17 +23,13 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
2123
protected $_uploaderFactory;
2224

2325
/**
24-
* Filesystem facade
25-
*
2626
* @var \Magento\Framework\Filesystem
2727
*
2828
* @deprecated
2929
*/
3030
protected $_filesystem;
3131

3232
/**
33-
* File Uploader factory
34-
*
3533
* @var \Magento\MediaStorage\Model\File\UploaderFactory
3634
*
3735
* @deprecated
@@ -46,15 +44,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
4644
protected $_logger;
4745

4846
/**
49-
* Image uploader
50-
*
5147
* @var \Magento\Catalog\Model\ImageUploader
5248
*/
5349
private $imageUploader;
5450

5551
/**
56-
* Image constructor.
57-
*
5852
* @param \Psr\Log\LoggerInterface $logger
5953
* @param \Magento\Framework\Filesystem $filesystem
6054
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
@@ -70,8 +64,50 @@ public function __construct(
7064
}
7165

7266
/**
73-
* Get image uploader
67+
* @param $value
68+
* @return string|bool
69+
*/
70+
protected function getUploadedImageName($value)
71+
{
72+
if (!is_array($value)) {
73+
return false;
74+
}
75+
76+
if (!count($value)) {
77+
return false;
78+
}
79+
80+
$imageData = reset($value);
81+
82+
if (!isset($imageData['name'])) {
83+
return false;
84+
}
85+
86+
return $imageData['name'];
87+
}
88+
89+
/**
90+
* Avoiding saving potential upload data to DB
7491
*
92+
* @param \Magento\Framework\DataObject $object
93+
* @return $this
94+
*/
95+
public function beforeSave($object)
96+
{
97+
$attributeName = $this->getAttribute()->getName();
98+
$value = $object->getData($attributeName);
99+
100+
if ($value === false || (is_array($value) && isset($value['delete']) && $value['delete'] === true)) {
101+
$object->setData($attributeName, '');
102+
} else if ($imageName = $this->getUploadedImageName($value)) {
103+
$object->setData($attributeName . self::ADDITIONAL_DATA_SUFFIX, $value);
104+
$object->setData($attributeName, $imageName);
105+
}
106+
107+
return parent::beforeSave($object);
108+
}
109+
110+
/**
75111
* @return \Magento\Catalog\Model\ImageUploader
76112
*
77113
* @deprecated
@@ -83,6 +119,7 @@ private function getImageUploader()
83119
\Magento\Catalog\CategoryImageUpload::class
84120
);
85121
}
122+
86123
return $this->imageUploader;
87124
}
88125

@@ -94,15 +131,18 @@ private function getImageUploader()
94131
*/
95132
public function afterSave($object)
96133
{
97-
$image = $object->getData($this->getAttribute()->getName(), null);
98-
99-
if ($image !== null) {
100-
try {
101-
$this->getImageUploader()->moveFileFromTmp($image);
102-
} catch (\Exception $e) {
103-
$this->_logger->critical($e);
104-
}
134+
$value = $object->getData($this->getAttribute()->getName() . self::ADDITIONAL_DATA_SUFFIX);
135+
136+
if (!$imageName = $this->getUploadedImageName($value)) {
137+
return $this;
105138
}
139+
140+
try {
141+
$this->getImageUploader()->moveFileFromTmp($imageName);
142+
} catch (\Exception $e) {
143+
$this->_logger->critical($e);
144+
}
145+
106146
return $this;
107147
}
108148
}

app/code/Magento/Catalog/Model/Category/DataProvider.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,20 @@ public function getData()
206206
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
207207
$categoryData = $this->addUseConfigSettings($categoryData);
208208
$categoryData = $this->filterFields($categoryData);
209-
if (isset($categoryData['image'])) {
210-
unset($categoryData['image']);
211-
$categoryData['image'][0]['name'] = $category->getData('image');
212-
$categoryData['image'][0]['url'] = $category->getImageUrl();
213-
}
209+
210+
foreach ($category->getAttributes() as $attributeCode => $attribute) {
211+
$backendModel = $attribute->getBackend();
212+
213+
if ($backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
214+
if (isset($categoryData[$attributeCode])) {
215+
unset($categoryData[$attributeCode]);
216+
217+
$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
218+
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
219+
}
220+
}
221+
}
222+
214223
$this->loadedData[$category->getId()] = $categoryData;
215224
}
216225
return $this->loadedData;

app/code/Magento/Ui/view/base/web/js/form/element/file-uploader.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,21 @@ define([
294294
/**
295295
* Handler which is invoked prior to the start of a file upload.
296296
*
297-
* @param {Event} e - Event obejct.
297+
* @param {Event} e - Event object.
298298
* @param {Object} data - File data that will be uploaded.
299299
*/
300300
onBeforeFileUpload: function (e, data) {
301301
var file = data.files[0],
302302
allowed = this.isFileAllowed(file);
303303

304304
if (allowed.passed) {
305-
$(e.target).fileupload('process', data).done(function () {
305+
var $target = $(e.target);
306+
307+
$target.on('fileuploadsend', function(event, postData) {
308+
postData.data.set('param_name', this.paramName);
309+
}.bind(data));
310+
311+
$target.fileupload('process', data).done(function () {
306312
data.submit();
307313
});
308314
} else {

0 commit comments

Comments
 (0)