7
7
8
8
namespace Magento \Catalog \Controller \Adminhtml \Product \Attribute ;
9
9
10
+ use Magento \Backend \App \Action \Context ;
11
+ use Magento \Backend \Model \View \Result \Redirect ;
12
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
13
+ use Magento \Catalog \Controller \Adminhtml \Product \Attribute ;
14
+ use Magento \Catalog \Helper \Product ;
15
+ use Magento \Catalog \Model \Product \AttributeSet \BuildFactory ;
16
+ use Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory ;
17
+ use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \Validator ;
18
+ use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \ValidatorFactory ;
19
+ use Magento \Eav \Model \Entity \Attribute \Set ;
20
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \CollectionFactory ;
21
+ use Magento \Framework \Cache \FrontendInterface ;
22
+ use Magento \Framework \Controller \Result \Json ;
10
23
use Magento \Framework \Controller \ResultFactory ;
11
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 ;
12
30
13
31
/**
14
32
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
33
*/
16
- class Save extends \ Magento \ Catalog \ Controller \ Adminhtml \ Product \ Attribute
34
+ class Save extends Attribute
17
35
{
18
36
/**
19
- * @var \Magento\Catalog\Model\Product\AttributeSet\ BuildFactory
37
+ * @var BuildFactory
20
38
*/
21
39
protected $ buildFactory ;
22
40
23
41
/**
24
- * @var \Magento\Framework\Filter\ FilterManager
42
+ * @var FilterManager
25
43
*/
26
44
protected $ filterManager ;
27
45
28
46
/**
29
- * @var \Magento\Catalog\Helper\ Product
47
+ * @var Product
30
48
*/
31
49
protected $ productHelper ;
32
50
33
51
/**
34
- * @var \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory
52
+ * @var AttributeFactory
35
53
*/
36
54
protected $ attributeFactory ;
37
55
38
56
/**
39
- * @var \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ ValidatorFactory
57
+ * @var ValidatorFactory
40
58
*/
41
59
protected $ validatorFactory ;
42
60
43
61
/**
44
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\ CollectionFactory
62
+ * @var CollectionFactory
45
63
*/
46
64
protected $ groupCollectionFactory ;
47
65
48
66
/**
49
- * @var \Magento\Framework\View\ LayoutFactory
67
+ * @var LayoutFactory
50
68
*/
51
69
private $ layoutFactory ;
52
70
53
71
/**
54
- * @param \Magento\Backend\App\Action\ Context $context
55
- * @param \Magento\Framework\Cache\ FrontendInterface $attributeLabelCache
56
- * @param \Magento\Framework\ Registry $coreRegistry
57
- * @param \Magento\Catalog\Model\Product\AttributeSet\ BuildFactory $buildFactory
58
- * @param \Magento\Framework\View\Result\ PageFactory $resultPageFactory
59
- * @param \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory $attributeFactory
60
- * @param \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ ValidatorFactory $validatorFactory
61
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\ CollectionFactory $groupCollectionFactory
62
- * @param \Magento\Framework\Filter\ FilterManager $filterManager
63
- * @param \Magento\Catalog\Helper\ Product $productHelper
64
- * @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
65
83
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
66
84
*/
67
85
public function __construct (
68
- \ Magento \ Backend \ App \ Action \ Context $ context ,
69
- \ Magento \ Framework \ Cache \ FrontendInterface $ attributeLabelCache ,
70
- \ Magento \ Framework \ Registry $ coreRegistry ,
71
- \ Magento \ Framework \ View \ Result \ PageFactory $ resultPageFactory ,
72
- \ Magento \ Catalog \ Model \ Product \ AttributeSet \ BuildFactory $ buildFactory ,
73
- \ Magento \ Catalog \ Model \ ResourceModel \ Eav \ AttributeFactory $ attributeFactory ,
74
- \ Magento \ Eav \ Model \ Adminhtml \ System \ Config \ Source \ Inputtype \ ValidatorFactory $ validatorFactory ,
75
- \ Magento \ Eav \ Model \ ResourceModel \ Entity \ Attribute \ Group \ CollectionFactory $ groupCollectionFactory ,
76
- \ Magento \ Framework \ Filter \ FilterManager $ filterManager ,
77
- \ Magento \ Catalog \ Helper \ Product $ productHelper ,
78
- \ 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
79
97
) {
80
98
parent ::__construct ($ context , $ attributeLabelCache , $ coreRegistry , $ resultPageFactory );
81
99
$ this ->buildFactory = $ buildFactory ;
@@ -88,7 +106,7 @@ public function __construct(
88
106
}
89
107
90
108
/**
91
- * @return \Magento\Backend\Model\View\Result\ Redirect
109
+ * @return Redirect
92
110
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
93
111
* @SuppressWarnings(PHPMD.NPathComplexity)
94
112
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -105,32 +123,43 @@ public function execute()
105
123
$ name = trim ($ name );
106
124
107
125
try {
108
- /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\ Set */
126
+ /** @var $attributeSet Set */
109
127
$ attributeSet = $ this ->buildFactory ->create ()
110
128
->setEntityTypeId ($ this ->_entityTypeId )
111
129
->setSkeletonId ($ setId )
112
130
->setName ($ name )
113
131
->getAttributeSet ();
114
132
} catch (AlreadyExistsException $ alreadyExists ) {
115
- $ this ->messageManager ->addError (__ ('An attribute set named \'%1 \' already exists. ' , $ name ));
133
+ $ this ->messageManager ->addErrorMessage (__ ('An attribute set named \'%1 \' already exists. ' , $ name ));
116
134
$ this ->_session ->setAttributeData ($ data );
117
135
return $ this ->returnResult ('catalog/*/edit ' , ['_current ' => true ], ['error ' => true ]);
118
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
119
- $ this ->messageManager ->addError ($ e ->getMessage ());
136
+ } catch (LocalizedException $ e ) {
137
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
120
138
} catch (\Exception $ e ) {
121
- $ 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
+ );
122
143
}
123
144
}
124
145
125
146
$ attributeId = $ this ->getRequest ()->getParam ('attribute_id ' );
126
- $ attributeCode = $ this ->getRequest ()->getParam ('attribute_code ' )
127
- ?: $ 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 ]);
128
157
if (strlen ($ attributeCode ) > 0 ) {
129
158
$ validatorAttrCode = new \Zend_Validate_Regex (
130
159
['pattern ' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u ' ]
131
160
);
132
161
if (!$ validatorAttrCode ->isValid ($ attributeCode )) {
133
- $ this ->messageManager ->addError (
162
+ $ this ->messageManager ->addErrorMessage (
134
163
__ (
135
164
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
136
165
'numbers (0-9) or underscore(_) in this field, first character should be a letter. ' ,
@@ -148,11 +177,11 @@ public function execute()
148
177
149
178
//validate frontend_input
150
179
if (isset ($ data ['frontend_input ' ])) {
151
- /** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ Validator */
180
+ /** @var $inputType Validator */
152
181
$ inputType = $ this ->validatorFactory ->create ();
153
182
if (!$ inputType ->isValid ($ data ['frontend_input ' ])) {
154
183
foreach ($ inputType ->getMessages () as $ message ) {
155
- $ this ->messageManager ->addError ($ message );
184
+ $ this ->messageManager ->addErrorMessage ($ message );
156
185
}
157
186
return $ this ->returnResult (
158
187
'catalog/*/edit ' ,
@@ -162,18 +191,14 @@ public function execute()
162
191
}
163
192
}
164
193
165
- /* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
166
- $ model = $ this ->attributeFactory ->create ();
167
-
168
194
if ($ attributeId ) {
169
- $ model ->load ($ attributeId );
170
195
if (!$ model ->getId ()) {
171
- $ this ->messageManager ->addError (__ ('This attribute no longer exists. ' ));
196
+ $ this ->messageManager ->addErrorMessage (__ ('This attribute no longer exists. ' ));
172
197
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
173
198
}
174
199
// entity type check
175
200
if ($ model ->getEntityTypeId () != $ this ->_entityTypeId ) {
176
- $ this ->messageManager ->addError (__ ('We can \'t update the attribute. ' ));
201
+ $ this ->messageManager ->addErrorMessage (__ ('We can \'t update the attribute. ' ));
177
202
$ this ->_session ->setAttributeData ($ data );
178
203
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => true ]);
179
204
}
@@ -193,7 +218,7 @@ public function execute()
193
218
);
194
219
}
195
220
196
- $ data += ['is_filterable ' => 0 , 'is_filterable_in_search ' => 0 , ' apply_to ' => [] ];
221
+ $ data += ['is_filterable ' => 0 , 'is_filterable_in_search ' => 0 ];
197
222
198
223
if ($ model ->getIsUserDefined () === null || $ model ->getIsUserDefined () != 0 ) {
199
224
$ data ['backend_type ' ] = $ model ->getBackendTypeByInput ($ data ['frontend_input ' ]);
@@ -241,7 +266,7 @@ public function execute()
241
266
242
267
try {
243
268
$ model ->save ();
244
- $ this ->messageManager ->addSuccess (__ ('You saved the product attribute. ' ));
269
+ $ this ->messageManager ->addSuccessMessage (__ ('You saved the product attribute. ' ));
245
270
246
271
$ this ->_attributeLabelCache ->clean ();
247
272
$ this ->_session ->setAttributeData (false );
@@ -265,7 +290,7 @@ public function execute()
265
290
}
266
291
return $ this ->returnResult ('catalog/*/ ' , [], ['error ' => false ]);
267
292
} catch (\Exception $ e ) {
268
- $ this ->messageManager ->addError ($ e ->getMessage ());
293
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
269
294
$ this ->_session ->setAttributeData ($ data );
270
295
return $ this ->returnResult (
271
296
'catalog/*/edit ' ,
@@ -281,7 +306,7 @@ public function execute()
281
306
* @param string $path
282
307
* @param array $params
283
308
* @param array $response
284
- * @return \Magento\Framework\Controller\Result\ Json|\Magento\Backend\Model\View\Result\ Redirect
309
+ * @return Json|Redirect
285
310
*/
286
311
private function returnResult ($ path = '' , array $ params = [], array $ response = [])
287
312
{
0 commit comments