Skip to content

Commit b5f29b4

Browse files
committed
Merge commit 'refs/pull/5978/head' of https://github.com/magento/magento2 into MAGETWO-57989
2 parents 9fbaad7 + c82aa4f commit b5f29b4

File tree

10 files changed

+679
-89
lines changed

10 files changed

+679
-89
lines changed

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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 31 additions & 35 deletions
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,29 @@ public function execute()
248233
}
249234

250235
/**
251-
* Image data preprocessing
252-
*
253-
* @param array $data
254-
*
255-
* @return array
236+
* @param array $data Category data
237+
* @return mixed
238+
* @throws \Magento\Framework\Exception\LocalizedException
256239
*/
257-
public function imagePreprocessing($data)
240+
public function imagePreprocessing(array $data)
258241
{
259-
if (empty($data['image'])) {
260-
unset($data['image']);
261-
$data['image']['delete'] = true;
242+
$entityType = $this->eavConfig->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);
243+
244+
foreach ($entityType->getAttributeCollection() as $attributeModel) {
245+
$attributeCode = $attributeModel->getAttributeCode();
246+
$backendModel = $attributeModel->getBackend();
247+
248+
if (isset($data[$attributeCode])) {
249+
continue;
250+
}
251+
252+
if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
253+
continue;
254+
}
255+
256+
$data[$attributeCode] = false;
262257
}
258+
263259
return $data;
264260
}
265261

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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 50 additions & 16 deletions
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_PREFIX = '_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,43 +44,78 @@ 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-
*
52+
* @var \Magento\Framework\ObjectManagerInterface
53+
*/
54+
private $objectManager;
55+
56+
/**
5857
* @param \Psr\Log\LoggerInterface $logger
5958
* @param \Magento\Framework\Filesystem $filesystem
6059
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
60+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
6161
*/
6262
public function __construct(
6363
\Psr\Log\LoggerInterface $logger,
6464
\Magento\Framework\Filesystem $filesystem,
65-
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
65+
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
66+
\Magento\Framework\ObjectManagerInterface $objectManager
6667
) {
6768
$this->_filesystem = $filesystem;
6869
$this->_fileUploaderFactory = $fileUploaderFactory;
6970
$this->_logger = $logger;
71+
$this->objectManager = $objectManager;
72+
}
73+
74+
/**
75+
* @param array $value Attribute value
76+
* @return string
77+
*/
78+
protected function getUploadedImageName($value)
79+
{
80+
if (is_array($value) && isset($value[0]['name'])) {
81+
return $value[0]['name'];
82+
}
83+
84+
return '';
7085
}
7186

7287
/**
73-
* Get image uploader
88+
* Avoiding saving potential upload data to DB
7489
*
90+
* @param \Magento\Framework\DataObject $object
91+
* @return $this
92+
*/
93+
public function beforeSave($object)
94+
{
95+
$attributeName = $this->getAttribute()->getName();
96+
$value = $object->getData($attributeName);
97+
98+
if ($imageName = $this->getUploadedImageName($value)) {
99+
$object->setData(self::ADDITIONAL_DATA_PREFIX . $attributeName, $value);
100+
$object->setData($attributeName, $imageName);
101+
} else if (!is_string($value)) {
102+
$object->setData($attributeName, '');
103+
}
104+
105+
return parent::beforeSave($object);
106+
}
107+
108+
/**
75109
* @return \Magento\Catalog\Model\ImageUploader
76110
*
77111
* @deprecated
78112
*/
79113
private function getImageUploader()
80114
{
81115
if ($this->imageUploader === null) {
82-
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
83-
\Magento\Catalog\CategoryImageUpload::class
84-
);
116+
$this->imageUploader = $this->objectManager->get(\Magento\Catalog\CategoryImageUpload::class);
85117
}
118+
86119
return $this->imageUploader;
87120
}
88121

@@ -94,15 +127,16 @@ private function getImageUploader()
94127
*/
95128
public function afterSave($object)
96129
{
97-
$image = $object->getData($this->getAttribute()->getName(), null);
130+
$value = $object->getData(self::ADDITIONAL_DATA_PREFIX . $this->getAttribute()->getName());
98131

99-
if ($image !== null) {
132+
if ($imageName = $this->getUploadedImageName($value)) {
100133
try {
101-
$this->getImageUploader()->moveFileFromTmp($image);
134+
$this->getImageUploader()->moveFileFromTmp($imageName);
102135
} catch (\Exception $e) {
103136
$this->_logger->critical($e);
104137
}
105138
}
139+
106140
return $this;
107141
}
108142
}

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Ui\DataProvider\EavValidationRules;
1818
use Magento\Catalog\Model\CategoryFactory;
1919
use Magento\Framework\Exception\NoSuchEntityException;
20+
use Magento\Catalog\Model\Category\Attribute\Backend\Image as ImageBackendModel;
2021

2122
/**
2223
* Class DataProvider
@@ -206,11 +207,8 @@ public function getData()
206207
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
207208
$categoryData = $this->addUseConfigSettings($categoryData);
208209
$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-
}
210+
$categoryData = $this->convertValues($category, $categoryData);
211+
214212
$this->loadedData[$category->getId()] = $categoryData;
215213
}
216214
return $this->loadedData;
@@ -371,6 +369,29 @@ protected function filterFields($categoryData)
371369
return array_diff_key($categoryData, array_flip($this->ignoreFields));
372370
}
373371

372+
/**
373+
* @param \Magento\Catalog\Model\Category $category
374+
* @param array $categoryData
375+
* @return array
376+
*/
377+
protected function convertValues($category, $categoryData)
378+
{
379+
foreach ($category->getAttributes() as $attributeCode => $attribute) {
380+
if (!isset($categoryData[$attributeCode])) {
381+
continue;
382+
}
383+
384+
if ($attribute->getBackend() instanceof ImageBackendModel) {
385+
unset($categoryData[$attributeCode]);
386+
387+
$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
388+
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
389+
}
390+
}
391+
392+
return $categoryData;
393+
}
394+
374395
/**
375396
* Category's fields default values
376397
*

0 commit comments

Comments
 (0)