6
6
namespace Magento \Catalog \Controller \Adminhtml \Category ;
7
7
8
8
use Magento \Store \Model \StoreManagerInterface ;
9
+ use \Magento \Catalog \Api \Data \CategoryAttributeInterface ;
9
10
10
11
/**
11
12
* Class Save
@@ -48,6 +49,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
48
49
*/
49
50
private $ storeManager ;
50
51
52
+ /**
53
+ * @var \Magento\Eav\Model\Config
54
+ */
55
+ private $ eavConfig ;
56
+
51
57
/**
52
58
* Constructor
53
59
*
@@ -56,43 +62,22 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
56
62
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
57
63
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
58
64
* @param StoreManagerInterface $storeManager
65
+ * @param \Magento\Eav\Model\Config $eavConfig
59
66
*/
60
67
public function __construct (
61
68
\Magento \Backend \App \Action \Context $ context ,
62
69
\Magento \Framework \Controller \Result \RawFactory $ resultRawFactory ,
63
70
\Magento \Framework \Controller \Result \JsonFactory $ resultJsonFactory ,
64
71
\Magento \Framework \View \LayoutFactory $ layoutFactory ,
65
- StoreManagerInterface $ storeManager
72
+ StoreManagerInterface $ storeManager ,
73
+ \Magento \Eav \Model \Config $ eavConfig
66
74
) {
67
75
parent ::__construct ($ context );
68
76
$ this ->resultRawFactory = $ resultRawFactory ;
69
77
$ this ->resultJsonFactory = $ resultJsonFactory ;
70
78
$ this ->layoutFactory = $ layoutFactory ;
71
79
$ 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 ;
96
81
}
97
82
98
83
/**
@@ -126,7 +111,7 @@ public function execute()
126
111
$ this ->storeManager ->setCurrentStore ($ store ->getCode ());
127
112
$ parentId = isset ($ categoryPostData ['parent ' ]) ? $ categoryPostData ['parent ' ] : null ;
128
113
if ($ categoryPostData ) {
129
- $ category ->addData ($ this -> _filterCategoryPostData ( $ categoryPostData) );
114
+ $ category ->addData ($ categoryPostData );
130
115
if ($ isNewCategory ) {
131
116
$ parentCategory = $ this ->getParentCategory ($ parentId , $ storeId );
132
117
$ category ->setPath ($ parentCategory ->getPath ());
@@ -248,18 +233,28 @@ public function execute()
248
233
}
249
234
250
235
/**
251
- * Image data preprocessing
252
- *
253
- * @param array $data
254
- *
236
+ * @param $data
255
237
* @return array
256
238
*/
257
239
public function imagePreprocessing ($ data )
258
240
{
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 ;
262
256
}
257
+
263
258
return $ data ;
264
259
}
265
260
0 commit comments