5
5
* See COPYING.txt for license details.
6
6
*/
7
7
8
- // @codingStandardsIgnoreFile
9
-
10
8
namespace Magento \Catalog \Controller \Adminhtml \Product \Attribute ;
11
9
10
+ use Magento \Backend \App \Action \Context ;
11
+ use Magento \Backend \Model \View \Result \Redirect ;
12
+ use Magento \Catalog \Controller \Adminhtml \Product \Attribute ;
13
+ use Magento \Catalog \Model \Product \AttributeSet \BuildFactory ;
14
+ use Magento \Catalog \Helper \Product ;
15
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
16
+ use Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory ;
17
+ use Magento \Eav \Model \Entity \Attribute \Set ;
18
+ use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \Validator ;
19
+ use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \ValidatorFactory ;
20
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \CollectionFactory ;
21
+ use Magento \Framework \Cache \FrontendInterface ;
12
22
use Magento \Framework \Controller \ResultFactory ;
23
+ use Magento \Framework \Controller \Result \Json ;
13
24
use Magento \Framework \Exception \AlreadyExistsException ;
25
+ use Magento \Framework \Exception \LocalizedException ;
26
+ use Magento \Framework \Filter \FilterManager ;
27
+ use Magento \Framework \Registry ;
28
+ use Magento \Framework \View \LayoutFactory ;
29
+ use Magento \Framework \View \Result \PageFactory ;
14
30
15
31
/**
16
32
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
33
*/
18
- class Save extends \ Magento \ Catalog \ Controller \ Adminhtml \ Product \ Attribute
34
+ class Save extends Attribute
19
35
{
20
36
/**
21
- * @var \Magento\Catalog\Model\Product\AttributeSet\ BuildFactory
37
+ * @var BuildFactory
22
38
*/
23
39
protected $ buildFactory ;
24
40
25
41
/**
26
- * @var \Magento\Framework\Filter\ FilterManager
42
+ * @var FilterManager
27
43
*/
28
44
protected $ filterManager ;
29
45
30
46
/**
31
- * @var \Magento\Catalog\Helper\ Product
47
+ * @var Product
32
48
*/
33
49
protected $ productHelper ;
34
50
35
51
/**
36
- * @var \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory
52
+ * @var AttributeFactory
37
53
*/
38
54
protected $ attributeFactory ;
39
55
40
56
/**
41
- * @var \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ ValidatorFactory
57
+ * @var ValidatorFactory
42
58
*/
43
59
protected $ validatorFactory ;
44
60
45
61
/**
46
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\ CollectionFactory
62
+ * @var CollectionFactory
47
63
*/
48
64
protected $ groupCollectionFactory ;
49
65
50
66
/**
51
- * @var \Magento\Framework\View\ LayoutFactory
67
+ * @var LayoutFactory
52
68
*/
53
69
private $ layoutFactory ;
54
70
55
71
/**
56
- * @param \Magento\Backend\App\Action\ Context $context
57
- * @param \Magento\Framework\Cache\ FrontendInterface $attributeLabelCache
58
- * @param \Magento\Framework\ Registry $coreRegistry
59
- * @param \Magento\Catalog\Model\Product\AttributeSet\ BuildFactory $buildFactory
60
- * @param \Magento\Framework\View\Result\ PageFactory $resultPageFactory
61
- * @param \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory $attributeFactory
62
- * @param \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ ValidatorFactory $validatorFactory
63
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\ CollectionFactory $groupCollectionFactory
64
- * @param \Magento\Framework\Filter\ FilterManager $filterManager
65
- * @param \Magento\Catalog\Helper\ Product $productHelper
66
- * @param \Magento\Framework\View\ LayoutFactory $layoutFactory
72
+ * @param Context $context
73
+ * @param FrontendInterface $attributeLabelCache
74
+ * @param Registry $coreRegistry
75
+ * @param BuildFactory $buildFactory
76
+ * @param PageFactory $resultPageFactory
77
+ * @param AttributeFactory $attributeFactory
78
+ * @param ValidatorFactory $validatorFactory
79
+ * @param CollectionFactory $groupCollectionFactory
80
+ * @param FilterManager $filterManager
81
+ * @param Product $productHelper
82
+ * @param LayoutFactory $layoutFactory
67
83
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
68
84
*/
69
85
public function __construct (
70
- \ Magento \ Backend \ App \ Action \ Context $ context ,
71
- \ Magento \ Framework \ Cache \ FrontendInterface $ attributeLabelCache ,
72
- \ Magento \ Framework \ Registry $ coreRegistry ,
73
- \ Magento \ Framework \ View \ Result \ PageFactory $ resultPageFactory ,
74
- \ Magento \ Catalog \ Model \ Product \ AttributeSet \ BuildFactory $ buildFactory ,
75
- \ Magento \ Catalog \ Model \ ResourceModel \ Eav \ AttributeFactory $ attributeFactory ,
76
- \ Magento \ Eav \ Model \ Adminhtml \ System \ Config \ Source \ Inputtype \ ValidatorFactory $ validatorFactory ,
77
- \ Magento \ Eav \ Model \ ResourceModel \ Entity \ Attribute \ Group \ CollectionFactory $ groupCollectionFactory ,
78
- \ Magento \ Framework \ Filter \ FilterManager $ filterManager ,
79
- \ Magento \ Catalog \ Helper \ Product $ productHelper ,
80
- \ Magento \ Framework \ View \ LayoutFactory $ layoutFactory
86
+ Context $ context ,
87
+ FrontendInterface $ attributeLabelCache ,
88
+ Registry $ coreRegistry ,
89
+ PageFactory $ resultPageFactory ,
90
+ BuildFactory $ buildFactory ,
91
+ AttributeFactory $ attributeFactory ,
92
+ ValidatorFactory $ validatorFactory ,
93
+ CollectionFactory $ groupCollectionFactory ,
94
+ FilterManager $ filterManager ,
95
+ Product $ productHelper ,
96
+ LayoutFactory $ layoutFactory
81
97
) {
82
98
parent ::__construct ($ context , $ attributeLabelCache , $ coreRegistry , $ resultPageFactory );
83
99
$ this ->buildFactory = $ buildFactory ;
@@ -90,7 +106,7 @@ public function __construct(
90
106
}
91
107
92
108
/**
93
- * @return \Magento\Backend\Model\View\Result\ Redirect
109
+ * @return Redirect
94
110
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
95
111
* @SuppressWarnings(PHPMD.NPathComplexity)
96
112
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -107,36 +123,51 @@ public function execute()
107
123
$ name = trim ($ name );
108
124
109
125
try {
110
- /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\ Set */
126
+ /** @var $attributeSet Set */
111
127
$ attributeSet = $ this ->buildFactory ->create ()
112
128
->setEntityTypeId ($ this ->_entityTypeId )
113
129
->setSkeletonId ($ setId )
114
130
->setName ($ name )
115
131
->getAttributeSet ();
116
132
} catch (AlreadyExistsException $ alreadyExists ) {
117
- $ this ->messageManager ->addError (__ ('An attribute set named \'%1 \' already exists. ' , $ name ));
133
+ $ this ->messageManager ->addErrorMessage (__ ('An attribute set named \'%1 \' already exists. ' , $ name ));
118
134
$ this ->_session ->setAttributeData ($ data );
135
+
119
136
return $ this ->returnResult ('catalog/*/edit ' , ['_current ' => true ], ['error ' => true ]);
120
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
121
- $ this ->messageManager ->addError ($ e ->getMessage ());
137
+ } catch (LocalizedException $ e ) {
138
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
122
139
} catch (\Exception $ e ) {
123
- $ this ->messageManager ->addException ($ e , __ ('Something went wrong while saving the attribute. ' ));
140
+ $ this ->messageManager ->addExceptionMessage (
141
+ $ e ,
142
+ __ ('Something went wrong while saving the attribute. ' )
143
+ );
124
144
}
125
145
}
126
146
127
147
$ attributeId = $ this ->getRequest ()->getParam ('attribute_id ' );
128
- $ attributeCode = $ this ->getRequest ()->getParam ('attribute_code ' )
129
- ?: $ this ->generateCode ($ this ->getRequest ()->getParam ('frontend_label ' )[0 ]);
148
+
149
+ /** @var $model ProductAttributeInterface */
150
+ $ model = $ this ->attributeFactory ->create ();
151
+ if ($ attributeId ) {
152
+ $ model ->load ($ attributeId );
153
+ }
154
+ $ attributeCode = $ model && $ model ->getId ()
155
+ ? $ model ->getAttributeCode ()
156
+ : $ this ->getRequest ()->getParam ('attribute_code ' );
157
+ $ attributeCode = $ attributeCode ?: $ this ->generateCode ($ this ->getRequest ()->getParam ('frontend_label ' )[0 ]);
130
158
if (strlen ($ attributeCode ) > 0 ) {
131
- $ validatorAttrCode = new \Zend_Validate_Regex (['pattern ' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u ' ]);
159
+ $ validatorAttrCode = new \Zend_Validate_Regex (
160
+ ['pattern ' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u ' ]
161
+ );
132
162
if (!$ validatorAttrCode ->isValid ($ attributeCode )) {
133
- $ this ->messageManager ->addError (
163
+ $ this ->messageManager ->addErrorMessage (
134
164
__ (
135
165
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
136
166
'numbers (0-9) or underscore(_) in this field, first character should be a letter. ' ,
137
167
$ attributeCode
138
168
)
139
169
);
170
+
140
171
return $ this ->returnResult (
141
172
'catalog/*/edit ' ,
142
173
['attribute_id ' => $ attributeId , '_current ' => true ],
@@ -148,12 +179,13 @@ public function execute()
148
179
149
180
//validate frontend_input
150
181
if (isset ($ data ['frontend_input ' ])) {
151
- /** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ Validator */
182
+ /** @var $inputType Validator */
152
183
$ inputType = $ this ->validatorFactory ->create ();
153
184
if (!$ inputType ->isValid ($ data ['frontend_input ' ])) {
154
185
foreach ($ inputType ->getMessages () as $ message ) {
155
- $ this ->messageManager ->addError ($ message );
186
+ $ this ->messageManager ->addErrorMessage ($ message );
156
187
}
188
+
157
189
return $ this ->returnResult (
158
190
'catalog/*/edit ' ,
159
191
['attribute_id ' => $ attributeId , '_current ' => true ],
@@ -162,19 +194,17 @@ public function execute()
162
194
}
163
195
}
164
196
165
- /* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
166
- $ model = $ this ->attributeFactory ->create ();
167
-
168
197
if ($ attributeId ) {
169
- $ model ->load ($ attributeId );
170
198
if (!$ model ->getId ()) {
171
- $ this ->messageManager ->addError (__ ('This attribute no longer exists. ' ));
199
+ $ this ->messageManager ->addErrorMessage (__ ('This attribute no longer exists. ' ));
200
+
172
201
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
173
202
}
174
203
// entity type check
175
204
if ($ model ->getEntityTypeId () != $ this ->_entityTypeId ) {
176
- $ this ->messageManager ->addError (__ ('We can \'t update the attribute. ' ));
205
+ $ this ->messageManager ->addErrorMessage (__ ('We can \'t update the attribute. ' ));
177
206
$ this ->_session ->setAttributeData ($ data );
207
+
178
208
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
179
209
}
180
210
@@ -195,7 +225,7 @@ public function execute()
195
225
196
226
$ data += ['is_filterable ' => 0 , 'is_filterable_in_search ' => 0 ];
197
227
198
- if (is_null ( $ model ->getIsUserDefined ()) || $ model ->getIsUserDefined () != 0 ) {
228
+ if ($ model ->getIsUserDefined () === null || $ model ->getIsUserDefined () != 0 ) {
199
229
$ data ['backend_type ' ] = $ model ->getBackendTypeByInput ($ data ['frontend_input ' ]);
200
230
}
201
231
@@ -241,7 +271,7 @@ public function execute()
241
271
242
272
try {
243
273
$ model ->save ();
244
- $ this ->messageManager ->addSuccess (__ ('You saved the product attribute. ' ));
274
+ $ this ->messageManager ->addSuccessMessage (__ ('You saved the product attribute. ' ));
245
275
246
276
$ this ->_attributeLabelCache ->clean ();
247
277
$ this ->_session ->setAttributeData (false );
@@ -252,9 +282,10 @@ public function execute()
252
282
'_current ' => true ,
253
283
'product_tab ' => $ this ->getRequest ()->getParam ('product_tab ' ),
254
284
];
255
- if (! is_null ( $ attributeSet) ) {
285
+ if ($ attributeSet !== null ) {
256
286
$ requestParams ['new_attribute_set_id ' ] = $ attributeSet ->getId ();
257
287
}
288
+
258
289
return $ this ->returnResult ('catalog/product/addAttribute ' , $ requestParams , ['error ' => false ]);
259
290
} elseif ($ this ->getRequest ()->getParam ('back ' , false )) {
260
291
return $ this ->returnResult (
@@ -263,25 +294,28 @@ public function execute()
263
294
['error ' => false ]
264
295
);
265
296
}
297
+
266
298
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => false ]);
267
299
} catch (\Exception $ e ) {
268
- $ this ->messageManager ->addError ($ e ->getMessage ());
300
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
269
301
$ this ->_session ->setAttributeData ($ data );
302
+
270
303
return $ this ->returnResult (
271
304
'catalog/*/edit ' ,
272
305
['attribute_id ' => $ attributeId , '_current ' => true ],
273
306
['error ' => true ]
274
307
);
275
308
}
276
309
}
310
+
277
311
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
278
312
}
279
313
280
314
/**
281
315
* @param string $path
282
316
* @param array $params
283
317
* @param array $response
284
- * @return \Magento\Framework\Controller\Result\ Json|\Magento\Backend\Model\View\Result\ Redirect
318
+ * @return Json|Redirect
285
319
*/
286
320
private function returnResult ($ path = '' , array $ params = [], array $ response = [])
287
321
{
@@ -291,8 +325,10 @@ private function returnResult($path = '', array $params = [], array $response =
291
325
292
326
$ response ['messages ' ] = [$ layout ->getMessagesBlock ()->getGroupedHtml ()];
293
327
$ response ['params ' ] = $ params ;
328
+
294
329
return $ this ->resultFactory ->create (ResultFactory::TYPE_JSON )->setData ($ response );
295
330
}
331
+
296
332
return $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT )->setPath ($ path , $ params );
297
333
}
298
334
0 commit comments