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
12
- use Magento \Framework \Exception \AlreadyExistsException ;
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 ;
13
22
use Magento \Framework \Controller \ResultFactory ;
23
+ use Magento \Framework \Controller \Result \Json ;
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,31 +123,43 @@ 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
-
117
132
} catch (AlreadyExistsException $ alreadyExists ) {
118
- $ this ->messageManager ->addError (__ ('An attribute set named \'%1 \' already exists. ' , $ name ));
133
+ $ this ->messageManager ->addErrorMessage (__ ('An attribute set named \'%1 \' already exists. ' , $ name ));
119
134
$ this ->_session ->setAttributeData ($ data );
120
135
return $ this ->returnResult ('catalog/*/edit ' , ['_current ' => true ], ['error ' => true ]);
121
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
122
- $ this ->messageManager ->addError ($ e ->getMessage ());
136
+ } catch (LocalizedException $ e ) {
137
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
123
138
} catch (\Exception $ e ) {
124
- $ this ->messageManager ->addException ($ e , __ ('Something went wrong while saving the attribute. ' ));
139
+ $ this ->messageManager ->addExceptionMessage (
140
+ $ e ,
141
+ __ ('Something went wrong while saving the attribute. ' )
142
+ );
125
143
}
126
144
}
127
145
128
146
$ attributeId = $ this ->getRequest ()->getParam ('attribute_id ' );
129
- $ attributeCode = $ this ->getRequest ()->getParam ('attribute_code ' )
130
- ?: $ this ->generateCode ($ this ->getRequest ()->getParam ('frontend_label ' )[0 ]);
147
+
148
+ /** @var $model ProductAttributeInterface */
149
+ $ model = $ this ->attributeFactory ->create ();
150
+ if ($ attributeId ) {
151
+ $ model ->load ($ attributeId );
152
+ }
153
+ $ attributeCode = $ model && $ model ->getId ()
154
+ ? $ model ->getAttributeCode ()
155
+ : $ this ->getRequest ()->getParam ('attribute_code ' );
156
+ $ attributeCode = $ attributeCode ?: $ this ->generateCode ($ this ->getRequest ()->getParam ('frontend_label ' )[0 ]);
131
157
if (strlen ($ attributeCode ) > 0 ) {
132
- $ validatorAttrCode = new \Zend_Validate_Regex (['pattern ' => '/^[a-z][a-z_0-9]{0,30}$/ ' ]);
158
+ $ validatorAttrCode = new \Zend_Validate_Regex (
159
+ ['pattern ' => '/^[a-z][a-z_0-9]{0,30}$/ ' ]
160
+ );
133
161
if (!$ validatorAttrCode ->isValid ($ attributeCode )) {
134
- $ this ->messageManager ->addError (
162
+ $ this ->messageManager ->addErrorMessage (
135
163
__ (
136
164
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
137
165
'numbers (0-9) or underscore(_) in this field, first character should be a letter. ' ,
@@ -149,11 +177,11 @@ public function execute()
149
177
150
178
//validate frontend_input
151
179
if (isset ($ data ['frontend_input ' ])) {
152
- /** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ Validator */
180
+ /** @var $inputType Validator */
153
181
$ inputType = $ this ->validatorFactory ->create ();
154
182
if (!$ inputType ->isValid ($ data ['frontend_input ' ])) {
155
183
foreach ($ inputType ->getMessages () as $ message ) {
156
- $ this ->messageManager ->addError ($ message );
184
+ $ this ->messageManager ->addErrorMessage ($ message );
157
185
}
158
186
return $ this ->returnResult (
159
187
'catalog/*/edit ' ,
@@ -163,18 +191,14 @@ public function execute()
163
191
}
164
192
}
165
193
166
- /* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
167
- $ model = $ this ->attributeFactory ->create ();
168
-
169
194
if ($ attributeId ) {
170
- $ model ->load ($ attributeId );
171
195
if (!$ model ->getId ()) {
172
- $ this ->messageManager ->addError (__ ('This attribute no longer exists. ' ));
196
+ $ this ->messageManager ->addErrorMessage (__ ('This attribute no longer exists. ' ));
173
197
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
174
198
}
175
199
// entity type check
176
200
if ($ model ->getEntityTypeId () != $ this ->_entityTypeId ) {
177
- $ this ->messageManager ->addError (__ ('We can \'t update the attribute. ' ));
201
+ $ this ->messageManager ->addErrorMessage (__ ('We can \'t update the attribute. ' ));
178
202
$ this ->_session ->setAttributeData ($ data );
179
203
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
180
204
}
@@ -196,7 +220,7 @@ public function execute()
196
220
197
221
$ data += ['is_filterable ' => 0 , 'is_filterable_in_search ' => 0 , 'apply_to ' => []];
198
222
199
- if (is_null ( $ model ->getIsUserDefined ()) || $ model ->getIsUserDefined () != 0 ) {
223
+ if ($ model ->getIsUserDefined () === null || $ model ->getIsUserDefined () != 0 ) {
200
224
$ data ['backend_type ' ] = $ model ->getBackendTypeByInput ($ data ['frontend_input ' ]);
201
225
}
202
226
@@ -242,7 +266,7 @@ public function execute()
242
266
243
267
try {
244
268
$ model ->save ();
245
- $ this ->messageManager ->addSuccess (__ ('You saved the product attribute. ' ));
269
+ $ this ->messageManager ->addSuccessMessage (__ ('You saved the product attribute. ' ));
246
270
247
271
$ this ->_attributeLabelCache ->clean ();
248
272
$ this ->_session ->setAttributeData (false );
@@ -253,7 +277,7 @@ public function execute()
253
277
'_current ' => true ,
254
278
'product_tab ' => $ this ->getRequest ()->getParam ('product_tab ' ),
255
279
];
256
- if (! is_null ( $ attributeSet) ) {
280
+ if ($ attributeSet !== null ) {
257
281
$ requestParams ['new_attribute_set_id ' ] = $ attributeSet ->getId ();
258
282
}
259
283
return $ this ->returnResult ('catalog/product/addAttribute ' , $ requestParams , ['error ' => false ]);
@@ -266,7 +290,7 @@ public function execute()
266
290
}
267
291
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => false ]);
268
292
} catch (\Exception $ e ) {
269
- $ this ->messageManager ->addError ($ e ->getMessage ());
293
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
270
294
$ this ->_session ->setAttributeData ($ data );
271
295
return $ this ->returnResult (
272
296
'catalog/*/edit ' ,
@@ -282,7 +306,7 @@ public function execute()
282
306
* @param string $path
283
307
* @param array $params
284
308
* @param array $response
285
- * @return \Magento\Framework\Controller\Result\ Json|\Magento\Backend\Model\View\Result\ Redirect
309
+ * @return Json|Redirect
286
310
*/
287
311
private function returnResult ($ path = '' , array $ params = [], array $ response = [])
288
312
{
@@ -295,7 +319,6 @@ private function returnResult($path = '', array $params = [], array $response =
295
319
return $ this ->resultFactory ->create (ResultFactory::TYPE_JSON )->setData ($ response );
296
320
}
297
321
return $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT )->setPath ($ path , $ params );
298
-
299
322
}
300
323
301
324
/**
0 commit comments