33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Catalog \Controller \Adminhtml \Product \Attribute ;
89
10+ use Magento \Backend \App \Action \Context ;
911use Magento \Catalog \Controller \Adminhtml \Product \Attribute as AttributeAction ;
12+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
13+ use Magento \Eav \Model \Entity \Attribute \Set ;
1014use Magento \Eav \Model \Validator \Attribute \Code as AttributeCodeValidator ;
1115use Magento \Framework \App \Action \HttpGetActionInterface ;
1216use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
1317use Magento \Framework \App \ObjectManager ;
18+ use Magento \Framework \Cache \FrontendInterface ;
19+ use Magento \Framework \Controller \Result \JsonFactory ;
20+ use Magento \Framework \Controller \ResultInterface ;
1421use Magento \Framework \DataObject ;
1522use Magento \Framework \Escaper ;
23+ use Magento \Framework \Registry ;
1624use Magento \Framework \Serialize \Serializer \FormData ;
25+ use Magento \Framework \View \LayoutFactory ;
26+ use Magento \Framework \View \Result \PageFactory ;
1727
1828/**
1929 * Product attribute validate controller.
@@ -25,12 +35,12 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
2535 const DEFAULT_MESSAGE_KEY = 'message ' ;
2636
2737 /**
28- * @var \Magento\Framework\Controller\Result\ JsonFactory
38+ * @var JsonFactory
2939 */
3040 protected $ resultJsonFactory ;
3141
3242 /**
33- * @var \Magento\Framework\View\ LayoutFactory
43+ * @var LayoutFactory
3444 */
3545 protected $ layoutFactory ;
3646
@@ -57,25 +67,25 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
5767 /**
5868 * Constructor
5969 *
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
6676 * @param array $multipleAttributeList
6777 * @param FormData|null $formDataSerializer
6878 * @param AttributeCodeValidator|null $attributeCodeValidator
6979 * @param Escaper $escaper
7080 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
7181 */
7282 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 ,
7989 array $ multipleAttributeList = [],
8090 FormData $ formDataSerializer = null ,
8191 AttributeCodeValidator $ attributeCodeValidator = null ,
@@ -96,7 +106,7 @@ public function __construct(
96106 /**
97107 * @inheritdoc
98108 *
99- * @return \Magento\Framework\Controller\ ResultInterface
109+ * @return ResultInterface
100110 * @SuppressWarnings(PHPMD.NPathComplexity)
101111 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
102112 */
@@ -118,14 +128,22 @@ public function execute()
118128
119129 $ attributeCode = $ this ->getRequest ()->getParam ('attribute_code ' );
120130 $ frontendLabel = $ this ->getRequest ()->getParam ('frontend_label ' );
121- $ attributeCode = $ attributeCode ?: $ this ->generateCode ($ frontendLabel [0 ]);
122131 $ 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+ }
129147
130148 if ($ attribute ->getId () && !$ attributeId || $ attributeCode === 'product_type ' || $ attributeCode === 'type_id ' ) {
131149 $ message = strlen ($ this ->getRequest ()->getParam ('attribute_code ' ))
@@ -145,8 +163,8 @@ public function execute()
145163
146164 if ($ this ->getRequest ()->has ('new_attribute_set_name ' )) {
147165 $ 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);
150168 $ attributeSet ->setEntityTypeId ($ this ->_entityTypeId )->load ($ setName , 'attribute_set_name ' );
151169 if ($ attributeSet ->getId ()) {
152170 $ setName = $ this ->escaper ->escapeHtml ($ setName );
@@ -252,7 +270,7 @@ private function checkUniqueOption(DataObject $response, array $options = null)
252270 private function checkEmptyOption (DataObject $ response , array $ optionsForCheck = null )
253271 {
254272 foreach ($ optionsForCheck as $ optionValues ) {
255- if (isset ($ optionValues [0 ]) && trim ($ optionValues [0 ]) == '' ) {
273+ if (isset ($ optionValues [0 ]) && trim (( string ) $ optionValues [0 ]) == '' ) {
256274 $ this ->setMessageToResponse ($ response , [__ ("The value of Admin scope can't be empty. " )]);
257275 $ response ->setError (true );
258276 }
0 commit comments