3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Backend \Block \Widget ;
7
8
9
+ use Magento \Backend \Block \Widget \Form \Element \ElementCreator ;
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Backend \Block \Template \Context ;
12
+ use Magento \Framework \Data \Form as DataForm ;
13
+ use Magento \Backend \Block \Widget \Form \Renderer \Element ;
14
+ use Magento \Backend \Block \Widget \Form \Renderer \Fieldset ;
15
+ use Magento \Backend \Block \Widget \Form \Renderer \Fieldset \Element as FieldsetElement ;
16
+ use Magento \Eav \Model \Entity \Attribute ;
17
+ use Magento \Framework \Data \Form \Element \AbstractElement ;
18
+ use Magento \Framework \Data \Form \AbstractForm ;
19
+
8
20
/**
9
21
* Backend form widget
10
22
*
@@ -18,7 +30,7 @@ class Form extends \Magento\Backend\Block\Widget
18
30
/**
19
31
* Form Object
20
32
*
21
- * @var \Magento\Framework\Data\Form
33
+ * @var DataForm
22
34
*/
23
35
protected $ _form ;
24
36
@@ -28,12 +40,24 @@ class Form extends \Magento\Backend\Block\Widget
28
40
protected $ _template = 'Magento_Backend::widget/form.phtml ' ;
29
41
30
42
/**
31
- * @param \Magento\Backend\Block\Template\Context $context
43
+ * @var ElementCreator
44
+ * /
45
+ private $creator;
46
+
47
+ /**
48
+ * Constructs form
49
+ *
50
+ * @param Context $context
32
51
* @param array $data
52
+ * @param ElementCreator|null $creator
33
53
*/
34
- public function __construct (\Magento \Backend \Block \Template \Context $ context , array $ data = [])
35
- {
54
+ public function __construct (
55
+ Context $ context ,
56
+ array $ data = [],
57
+ ElementCreator $ creator = null
58
+ ) {
36
59
parent ::__construct ($ context , $ data );
60
+ $ this ->creator = $ creator ?: ObjectManager::getInstance ()->get (ElementCreator::class);
37
61
}
38
62
39
63
/**
@@ -58,21 +82,21 @@ protected function _construct()
58
82
*/
59
83
protected function _prepareLayout ()
60
84
{
61
- \ Magento \ Framework \ Data \Form ::setElementRenderer (
85
+ DataForm ::setElementRenderer (
62
86
$ this ->getLayout ()->createBlock (
63
- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Element::class,
87
+ Element::class,
64
88
$ this ->getNameInLayout () . '_element '
65
89
)
66
90
);
67
- \ Magento \ Framework \ Data \Form ::setFieldsetRenderer (
91
+ DataForm ::setFieldsetRenderer (
68
92
$ this ->getLayout ()->createBlock (
69
- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset::class,
93
+ Fieldset::class,
70
94
$ this ->getNameInLayout () . '_fieldset '
71
95
)
72
96
);
73
- \ Magento \ Framework \ Data \Form ::setFieldsetElementRenderer (
97
+ DataForm ::setFieldsetElementRenderer (
74
98
$ this ->getLayout ()->createBlock (
75
- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset \Element ::class,
99
+ FieldsetElement ::class,
76
100
$ this ->getNameInLayout () . '_fieldset_element '
77
101
)
78
102
);
@@ -83,7 +107,7 @@ protected function _prepareLayout()
83
107
/**
84
108
* Get form object
85
109
*
86
- * @return \Magento\Framework\Data\Form
110
+ * @return DataForm
87
111
*/
88
112
public function getForm ()
89
113
{
@@ -106,10 +130,10 @@ public function getFormHtml()
106
130
/**
107
131
* Set form object
108
132
*
109
- * @param \Magento\Framework\Data\Form $form
133
+ * @param DataForm $form
110
134
* @return $this
111
135
*/
112
- public function setForm (\ Magento \ Framework \ Data \ Form $ form )
136
+ public function setForm (DataForm $ form )
113
137
{
114
138
$ this ->_form = $ form ;
115
139
$ this ->_form ->setParent ($ this );
@@ -148,6 +172,7 @@ protected function _beforeToHtml()
148
172
149
173
/**
150
174
* Initialize form fields values
175
+ *
151
176
* Method will be called after prepareForm and can be used for field values initialization
152
177
*
153
178
* @return $this
@@ -169,36 +194,15 @@ protected function _setFieldset($attributes, $fieldset, $exclude = [])
169
194
{
170
195
$ this ->_addElementTypes ($ fieldset );
171
196
foreach ($ attributes as $ attribute ) {
172
- /* @var $attribute \Magento\Eav\Model\Entity\ Attribute */
197
+ /* @var $attribute Attribute */
173
198
if (!$ this ->_isAttributeVisible ($ attribute )) {
174
199
continue ;
175
200
}
176
- if (($ inputType = $ attribute ->getFrontend ()->getInputType ()) && !in_array (
177
- $ attribute ->getAttributeCode (),
178
- $ exclude
179
- ) && ('media_image ' != $ inputType || $ attribute ->getAttributeCode () == 'image ' )
201
+ if (($ inputType = $ attribute ->getFrontend ()->getInputType ())
202
+ && !in_array ($ attribute ->getAttributeCode (), $ exclude )
203
+ && ('media_image ' !== $ inputType || $ attribute ->getAttributeCode () == 'image ' )
180
204
) {
181
- $ fieldType = $ inputType ;
182
- $ rendererClass = $ attribute ->getFrontend ()->getInputRendererClass ();
183
- if (!empty ($ rendererClass )) {
184
- $ fieldType = $ inputType . '_ ' . $ attribute ->getAttributeCode ();
185
- $ fieldset ->addType ($ fieldType , $ rendererClass );
186
- }
187
-
188
- $ element = $ fieldset ->addField (
189
- $ attribute ->getAttributeCode (),
190
- $ fieldType ,
191
- [
192
- 'name ' => $ attribute ->getAttributeCode (),
193
- 'label ' => $ attribute ->getFrontend ()->getLocalizedLabel (),
194
- 'class ' => $ attribute ->getFrontend ()->getClass (),
195
- 'required ' => $ attribute ->getIsRequired (),
196
- 'note ' => $ attribute ->getNote ()
197
- ]
198
- )->setEntityAttribute (
199
- $ attribute
200
- );
201
-
205
+ $ element = $ this ->creator ->create ($ fieldset , $ attribute );
202
206
$ element ->setAfterElementHtml ($ this ->_getAdditionalElementHtml ($ element ));
203
207
204
208
$ this ->_applyTypeSpecificConfig ($ inputType , $ element , $ attribute );
@@ -209,10 +213,10 @@ protected function _setFieldset($attributes, $fieldset, $exclude = [])
209
213
/**
210
214
* Check whether attribute is visible
211
215
*
212
- * @param \Magento\Eav\Model\Entity\ Attribute $attribute
216
+ * @param Attribute $attribute
213
217
* @return bool
214
218
*/
215
- protected function _isAttributeVisible (\ Magento \ Eav \ Model \ Entity \ Attribute $ attribute )
219
+ protected function _isAttributeVisible (Attribute $ attribute )
216
220
{
217
221
return !(!$ attribute || $ attribute ->hasIsVisible () && !$ attribute ->getIsVisible ());
218
222
}
@@ -221,11 +225,11 @@ protected function _isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attr
221
225
* Apply configuration specific for different element type
222
226
*
223
227
* @param string $inputType
224
- * @param \Magento\Framework\Data\Form\Element\ AbstractElement $element
225
- * @param \Magento\Eav\Model\Entity\ Attribute $attribute
228
+ * @param AbstractElement $element
229
+ * @param Attribute $attribute
226
230
* @return void
227
231
*/
228
- protected function _applyTypeSpecificConfig ($ inputType , $ element , \ Magento \ Eav \ Model \ Entity \ Attribute $ attribute )
232
+ protected function _applyTypeSpecificConfig ($ inputType , $ element , Attribute $ attribute )
229
233
{
230
234
switch ($ inputType ) {
231
235
case 'select ' :
@@ -249,10 +253,10 @@ protected function _applyTypeSpecificConfig($inputType, $element, \Magento\Eav\M
249
253
/**
250
254
* Add new element type
251
255
*
252
- * @param \Magento\Framework\Data\Form\ AbstractForm $baseElement
256
+ * @param AbstractForm $baseElement
253
257
* @return void
254
258
*/
255
- protected function _addElementTypes (\ Magento \ Framework \ Data \ Form \ AbstractForm $ baseElement )
259
+ protected function _addElementTypes (AbstractForm $ baseElement )
256
260
{
257
261
$ types = $ this ->_getAdditionalElementTypes ();
258
262
foreach ($ types as $ code => $ className ) {
@@ -273,7 +277,7 @@ protected function _getAdditionalElementTypes()
273
277
/**
274
278
* Render additional element
275
279
*
276
- * @param \Magento\Framework\Data\Form\Element\ AbstractElement $element
280
+ * @param AbstractElement $element
277
281
* @return string
278
282
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
279
283
*/
0 commit comments