3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Controller \Adminhtml \Product \Attribute ;
8
9
10
+ use Magento \Backend \App \Action \Context ;
9
11
use Magento \Catalog \Controller \Adminhtml \Product \Attribute as AttributeAction ;
12
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
13
+ use Magento \Eav \Model \Entity \Attribute \Set ;
10
14
use Magento \Eav \Model \Validator \Attribute \Code as AttributeCodeValidator ;
11
15
use Magento \Framework \App \Action \HttpGetActionInterface ;
12
16
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
13
17
use Magento \Framework \App \ObjectManager ;
18
+ use Magento \Framework \Cache \FrontendInterface ;
19
+ use Magento \Framework \Controller \Result \JsonFactory ;
20
+ use Magento \Framework \Controller \ResultInterface ;
14
21
use Magento \Framework \DataObject ;
15
22
use Magento \Framework \Escaper ;
23
+ use Magento \Framework \Registry ;
16
24
use Magento \Framework \Serialize \Serializer \FormData ;
25
+ use Magento \Framework \View \LayoutFactory ;
26
+ use Magento \Framework \View \Result \PageFactory ;
17
27
18
28
/**
19
29
* Product attribute validate controller.
@@ -25,12 +35,12 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
25
35
const DEFAULT_MESSAGE_KEY = 'message ' ;
26
36
27
37
/**
28
- * @var \Magento\Framework\Controller\Result\ JsonFactory
38
+ * @var JsonFactory
29
39
*/
30
40
protected $ resultJsonFactory ;
31
41
32
42
/**
33
- * @var \Magento\Framework\View\ LayoutFactory
43
+ * @var LayoutFactory
34
44
*/
35
45
protected $ layoutFactory ;
36
46
@@ -57,25 +67,25 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
57
67
/**
58
68
* Constructor
59
69
*
60
- * @param \Magento\Backend\App\Action\ Context $context
61
- * @param \Magento\Framework\Cache\ FrontendInterface $attributeLabelCache
62
- * @param \Magento\Framework\ Registry $coreRegistry
63
- * @param \Magento\Framework\View\Result\ PageFactory $resultPageFactory
64
- * @param \Magento\Framework\Controller\Result\ JsonFactory $resultJsonFactory
65
- * @param \Magento\Framework\View\ LayoutFactory $layoutFactory
70
+ * @param Context $context
71
+ * @param FrontendInterface $attributeLabelCache
72
+ * @param Registry $coreRegistry
73
+ * @param PageFactory $resultPageFactory
74
+ * @param JsonFactory $resultJsonFactory
75
+ * @param LayoutFactory $layoutFactory
66
76
* @param array $multipleAttributeList
67
77
* @param FormData|null $formDataSerializer
68
78
* @param AttributeCodeValidator|null $attributeCodeValidator
69
79
* @param Escaper $escaper
70
80
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
71
81
*/
72
82
public function __construct (
73
- \ Magento \ Backend \ App \ Action \ Context $ context ,
74
- \ Magento \ Framework \ Cache \ FrontendInterface $ attributeLabelCache ,
75
- \ Magento \ Framework \ Registry $ coreRegistry ,
76
- \ Magento \ Framework \ View \ Result \ PageFactory $ resultPageFactory ,
77
- \ Magento \ Framework \ Controller \ Result \ JsonFactory $ resultJsonFactory ,
78
- \ Magento \ Framework \ View \ LayoutFactory $ layoutFactory ,
83
+ Context $ context ,
84
+ FrontendInterface $ attributeLabelCache ,
85
+ Registry $ coreRegistry ,
86
+ PageFactory $ resultPageFactory ,
87
+ JsonFactory $ resultJsonFactory ,
88
+ LayoutFactory $ layoutFactory ,
79
89
array $ multipleAttributeList = [],
80
90
FormData $ formDataSerializer = null ,
81
91
AttributeCodeValidator $ attributeCodeValidator = null ,
@@ -96,7 +106,7 @@ public function __construct(
96
106
/**
97
107
* @inheritdoc
98
108
*
99
- * @return \Magento\Framework\Controller\ ResultInterface
109
+ * @return ResultInterface
100
110
* @SuppressWarnings(PHPMD.NPathComplexity)
101
111
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
102
112
*/
@@ -118,14 +128,22 @@ public function execute()
118
128
119
129
$ attributeCode = $ this ->getRequest ()->getParam ('attribute_code ' );
120
130
$ frontendLabel = $ this ->getRequest ()->getParam ('frontend_label ' );
121
- $ attributeCode = $ attributeCode ?: $ this ->generateCode ($ frontendLabel [0 ]);
122
131
$ attributeId = $ this ->getRequest ()->getParam ('attribute_id ' );
123
- $ attribute = $ this ->_objectManager ->create (
124
- \Magento \Catalog \Model \ResourceModel \Eav \Attribute::class
125
- )->loadByCode (
126
- $ this ->_entityTypeId ,
127
- $ attributeCode
128
- );
132
+
133
+ if ($ attributeId ) {
134
+ $ attribute = $ this ->_objectManager ->create (
135
+ Attribute::class
136
+ )->load ($ attributeId );
137
+ $ attributeCode = $ attribute ->getAttributeCode ();
138
+ } else {
139
+ $ attributeCode = $ attributeCode ?: $ this ->generateCode ($ frontendLabel [0 ]);
140
+ $ attribute = $ this ->_objectManager ->create (
141
+ Attribute::class
142
+ )->loadByCode (
143
+ $ this ->_entityTypeId ,
144
+ $ attributeCode
145
+ );
146
+ }
129
147
130
148
if ($ attribute ->getId () && !$ attributeId || $ attributeCode === 'product_type ' || $ attributeCode === 'type_id ' ) {
131
149
$ message = strlen ($ this ->getRequest ()->getParam ('attribute_code ' ))
@@ -145,8 +163,8 @@ public function execute()
145
163
146
164
if ($ this ->getRequest ()->has ('new_attribute_set_name ' )) {
147
165
$ setName = $ this ->getRequest ()->getParam ('new_attribute_set_name ' );
148
- /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\ Set */
149
- $ attributeSet = $ this ->_objectManager ->create (\ Magento \ Eav \ Model \ Entity \ Attribute \ Set::class);
166
+ /** @var $attributeSet Set */
167
+ $ attributeSet = $ this ->_objectManager ->create (Set::class);
150
168
$ attributeSet ->setEntityTypeId ($ this ->_entityTypeId )->load ($ setName , 'attribute_set_name ' );
151
169
if ($ attributeSet ->getId ()) {
152
170
$ setName = $ this ->escaper ->escapeHtml ($ setName );
@@ -252,7 +270,7 @@ private function checkUniqueOption(DataObject $response, array $options = null)
252
270
private function checkEmptyOption (DataObject $ response , array $ optionsForCheck = null )
253
271
{
254
272
foreach ($ optionsForCheck as $ optionValues ) {
255
- if (isset ($ optionValues [0 ]) && trim ($ optionValues [0 ]) == '' ) {
273
+ if (isset ($ optionValues [0 ]) && trim (( string ) $ optionValues [0 ]) == '' ) {
256
274
$ this ->setMessageToResponse ($ response , [__ ("The value of Admin scope can't be empty. " )]);
257
275
$ response ->setError (true );
258
276
}
0 commit comments