7
7
/**
8
8
* Product attribute add/edit form main tab
9
9
*
10
- * @author Magento Core Team <[email protected] >
10
+ * @author Magento Core Team <[email protected] >
11
11
*/
12
12
namespace Magento \Catalog \Block \Adminhtml \Product \Attribute \Edit \Tab ;
13
13
14
+ use Magento \Catalog \Block \Adminhtml \Product \Helper \Form \Apply as HelperApply ;
14
15
use Magento \Eav \Block \Adminhtml \Attribute \Edit \Main \AbstractMain ;
16
+ use Magento \Framework \Data \Form \Element \AbstractElement ;
17
+ use Magento \Framework \Data \Form \Element \Fieldset ;
18
+ use Magento \Framework \DataObject ;
15
19
16
20
/**
21
+ * Product attribute add/edit form main tab
22
+ *
17
23
* @api
18
- * @SuppressWarnings(PHPMD.DepthOfInheritance)
19
24
* @since 100.0.2
20
25
*/
21
26
class Main extends AbstractMain
22
27
{
23
28
/**
24
- * Adding product form elements for editing attribute
25
- *
26
- * @return $this
27
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
29
+ * @inheritdoc
28
30
*/
29
31
protected function _prepareForm ()
30
32
{
31
33
parent ::_prepareForm ();
32
- /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeObject */
33
- $ attributeObject = $ this ->getAttributeObject ();
34
- /* @var $form \Magento\Framework\Data\Form */
35
- $ form = $ this ->getForm ();
36
- /* @var $fieldset \Magento\Framework\Data\Form\Element\Fieldset */
37
- $ fieldset = $ form ->getElement ('base_fieldset ' );
38
- $ fieldsToRemove = ['attribute_code ' , 'is_unique ' , 'frontend_class ' ];
39
34
40
- foreach ($ fieldset ->getElements () as $ element ) {
41
- /** @var \Magento\Framework\Data\Form\AbstractForm $element */
42
- if (substr ($ element ->getId (), 0 , strlen ('default_value ' )) == 'default_value ' ) {
43
- $ fieldsToRemove [] = $ element ->getId ();
44
- }
45
- }
46
- foreach ($ fieldsToRemove as $ id ) {
47
- $ fieldset ->removeField ($ id );
48
- }
35
+ $ this ->removeUnusedFields ();
36
+ $ this ->processFrontendInputTypes ();
37
+
38
+ $ this ->_eventManager ->dispatch ('product_attribute_form_build_main_tab ' , ['form ' => $ this ->getForm ()]);
39
+
40
+ return $ this ;
41
+ }
49
42
43
+ /**
44
+ * @inheritdoc
45
+ */
46
+ protected function _getAdditionalElementTypes ()
47
+ {
48
+ return ['apply ' => HelperApply::class];
49
+ }
50
+
51
+ /**
52
+ * Process frontend input types for product attributes
53
+ *
54
+ * @return void
55
+ */
56
+ private function processFrontendInputTypes (): void
57
+ {
58
+ $ form = $ this ->getForm ();
59
+ /** @var AbstractElement $frontendInputElm */
50
60
$ frontendInputElm = $ form ->getElement ('frontend_input ' );
51
- $ additionalTypes = [
52
- ['value ' => 'price ' , 'label ' => __ ('Price ' )],
53
- ['value ' => 'media_image ' , 'label ' => __ ('Media Image ' )],
54
- ];
55
- $ additionalReadOnlyTypes = ['gallery ' => __ ('Gallery ' )];
56
- if (isset ($ additionalReadOnlyTypes [$ attributeObject ->getFrontendInput ()])) {
57
- $ additionalTypes [] = [
58
- 'value ' => $ attributeObject ->getFrontendInput (),
59
- 'label ' => $ additionalReadOnlyTypes [$ attributeObject ->getFrontendInput ()],
60
- ];
61
- }
61
+ $ additionalTypes = $ this ->getAdditionalFrontendInputTypes ();
62
62
63
- $ response = new \ Magento \ Framework \ DataObject ();
63
+ $ response = new DataObject ();
64
64
$ response ->setTypes ([]);
65
65
$ this ->_eventManager ->dispatch ('adminhtml_product_attribute_types ' , ['response ' => $ response ]);
66
66
$ _hiddenFields = [];
@@ -74,19 +74,51 @@ protected function _prepareForm()
74
74
75
75
$ frontendInputValues = array_merge ($ frontendInputElm ->getValues (), $ additionalTypes );
76
76
$ frontendInputElm ->setValues ($ frontendInputValues );
77
+ }
77
78
78
- $ this ->_eventManager ->dispatch ('product_attribute_form_build_main_tab ' , ['form ' => $ form ]);
79
+ /**
80
+ * Get additional Frontend Input Types for product attributes
81
+ *
82
+ * @return array
83
+ */
84
+ private function getAdditionalFrontendInputTypes (): array
85
+ {
86
+ $ additionalTypes = [
87
+ ['value ' => 'price ' , 'label ' => __ ('Price ' )],
88
+ ['value ' => 'media_image ' , 'label ' => __ ('Media Image ' )],
89
+ ];
79
90
80
- return $ this ;
91
+ $ additionalReadOnlyTypes = ['gallery ' => __ ('Gallery ' )];
92
+ $ attributeObject = $ this ->getAttributeObject ();
93
+ if (isset ($ additionalReadOnlyTypes [$ attributeObject ->getFrontendInput ()])) {
94
+ $ additionalTypes [] = [
95
+ 'value ' => $ attributeObject ->getFrontendInput (),
96
+ 'label ' => $ additionalReadOnlyTypes [$ attributeObject ->getFrontendInput ()],
97
+ ];
98
+ }
99
+
100
+ return $ additionalTypes ;
81
101
}
82
102
83
103
/**
84
- * Retrieve additional element types for product attributes
104
+ * Remove unused form fields
85
105
*
86
- * @return array
106
+ * @return void
87
107
*/
88
- protected function _getAdditionalElementTypes ()
108
+ private function removeUnusedFields (): void
89
109
{
90
- return ['apply ' => \Magento \Catalog \Block \Adminhtml \Product \Helper \Form \Apply::class];
110
+ $ form = $ this ->getForm ();
111
+ /* @var $fieldset Fieldset */
112
+ $ fieldset = $ form ->getElement ('base_fieldset ' );
113
+ $ fieldsToRemove = ['attribute_code ' , 'is_unique ' , 'frontend_class ' ];
114
+ foreach ($ fieldset ->getElements () as $ element ) {
115
+ /** @var AbstractElement $element */
116
+ if (substr ($ element ->getId (), 0 , strlen ('default_value ' )) == 'default_value ' ) {
117
+ $ fieldsToRemove [] = $ element ->getId ();
118
+ }
119
+ }
120
+ foreach ($ fieldsToRemove as $ id ) {
121
+ $ fieldset ->removeField ($ id );
122
+ }
91
123
}
92
124
}
0 commit comments