5
5
*/
6
6
namespace Magento \Sales \Block \Adminhtml \Order \Create \Form ;
7
7
8
+ use IntlDateFormatter ;
9
+ use Magento \Backend \Block \Template \Context ;
10
+ use Magento \Backend \Block \Widget \Form \Renderer \Element ;
11
+ use Magento \Backend \Block \Widget \Form \Renderer \Fieldset ;
12
+ use Magento \Backend \Model \Session \Quote ;
13
+ use Magento \Customer \Api \Data \OptionInterface ;
14
+ use Magento \Customer \Block \Adminhtml \Edit \Renderer \Region ;
15
+ use Magento \Customer \Block \Adminhtml \Form \Element \Boolean ;
16
+ use Magento \Customer \Block \Adminhtml \Form \Element \File ;
17
+ use Magento \Customer \Block \Adminhtml \Form \Element \Image ;
18
+ use Magento \Framework \Data \Form ;
19
+ use Magento \Framework \Data \Form \Element \AbstractElement ;
20
+ use Magento \Framework \Data \FormFactory ;
21
+ use Magento \Framework \Exception \LocalizedException ;
8
22
use Magento \Framework \Pricing \PriceCurrencyInterface ;
9
23
use Magento \Customer \Api \Data \AttributeMetadataInterface ;
24
+ use Magento \Framework \Reflection \DataObjectProcessor ;
25
+ use Magento \Sales \Block \Adminhtml \Order \Create \AbstractCreate ;
26
+ use Magento \Sales \Model \AdminOrder \Create ;
10
27
11
28
/**
12
29
* Sales Order Create Form Abstract Block
13
30
*
14
31
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
32
*/
16
- abstract class AbstractForm extends \ Magento \ Sales \ Block \ Adminhtml \ Order \ Create \ AbstractCreate
33
+ abstract class AbstractForm extends AbstractCreate
17
34
{
18
35
/**
19
- * Form factory
20
- *
21
- * @var \Magento\Framework\Data\FormFactory
36
+ * @var FormFactory
22
37
*/
23
38
protected $ _formFactory ;
24
39
25
40
/**
26
41
* Data Form object
27
42
*
28
- * @var \Magento\Framework\Data\ Form
43
+ * @var Form
29
44
*/
30
45
protected $ _form ;
31
46
32
47
/**
33
- * @var \Magento\Framework\Reflection\ DataObjectProcessor
48
+ * @var DataObjectProcessor
34
49
*/
35
50
protected $ dataObjectProcessor ;
36
51
37
52
/**
38
- * @param \Magento\Backend\Block\Template\ Context $context
39
- * @param \Magento\Backend\Model\Session\ Quote $sessionQuote
40
- * @param \Magento\Sales\Model\AdminOrder\ Create $orderCreate
53
+ * @param Context $context
54
+ * @param Quote $sessionQuote
55
+ * @param Create $orderCreate
41
56
* @param PriceCurrencyInterface $priceCurrency
42
- * @param \Magento\Framework\Data\ FormFactory $formFactory
43
- * @param \Magento\Framework\Reflection\ DataObjectProcessor $dataObjectProcessor
57
+ * @param FormFactory $formFactory
58
+ * @param DataObjectProcessor $dataObjectProcessor
44
59
* @param array $data
45
60
*/
46
61
public function __construct (
47
- \ Magento \ Backend \ Block \ Template \ Context $ context ,
48
- \ Magento \ Backend \ Model \ Session \ Quote $ sessionQuote ,
49
- \ Magento \ Sales \ Model \ AdminOrder \ Create $ orderCreate ,
62
+ Context $ context ,
63
+ Quote $ sessionQuote ,
64
+ Create $ orderCreate ,
50
65
PriceCurrencyInterface $ priceCurrency ,
51
- \ Magento \ Framework \ Data \ FormFactory $ formFactory ,
52
- \ Magento \ Framework \ Reflection \ DataObjectProcessor $ dataObjectProcessor ,
66
+ FormFactory $ formFactory ,
67
+ DataObjectProcessor $ dataObjectProcessor ,
53
68
array $ data = []
54
69
) {
55
70
$ this ->_formFactory = $ formFactory ;
@@ -61,26 +76,27 @@ public function __construct(
61
76
* Prepare global layout. Add renderers to \Magento\Framework\Data\Form
62
77
*
63
78
* @return $this
79
+ * @throws LocalizedException
64
80
*/
65
81
protected function _prepareLayout ()
66
82
{
67
83
parent ::_prepareLayout ();
68
84
69
- \ Magento \ Framework \ Data \ Form::setElementRenderer (
85
+ Form::setElementRenderer (
70
86
$ this ->getLayout ()->createBlock (
71
- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Element::class,
87
+ Element::class,
72
88
$ this ->getNameInLayout () . '_element '
73
89
)
74
90
);
75
- \ Magento \ Framework \ Data \ Form::setFieldsetRenderer (
91
+ Form::setFieldsetRenderer (
76
92
$ this ->getLayout ()->createBlock (
77
- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset::class,
93
+ Fieldset::class,
78
94
$ this ->getNameInLayout () . '_fieldset '
79
95
)
80
96
);
81
- \ Magento \ Framework \ Data \ Form::setFieldsetElementRenderer (
97
+ Form::setFieldsetElementRenderer (
82
98
$ this ->getLayout ()->createBlock (
83
- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset \Element::class,
99
+ Fieldset \Element::class,
84
100
$ this ->getNameInLayout () . '_fieldset_element '
85
101
)
86
102
);
@@ -91,7 +107,8 @@ protected function _prepareLayout()
91
107
/**
92
108
* Return Form object
93
109
*
94
- * @return \Magento\Framework\Data\Form
110
+ * @return Form
111
+ * @throws LocalizedException
95
112
*/
96
113
public function getForm ()
97
114
{
@@ -117,34 +134,35 @@ abstract protected function _prepareForm();
117
134
protected function _getAdditionalFormElementTypes ()
118
135
{
119
136
return [
120
- 'file ' => \ Magento \ Customer \ Block \ Adminhtml \ Form \ Element \ File::class,
121
- 'image ' => \ Magento \ Customer \ Block \ Adminhtml \ Form \ Element \ Image::class,
122
- 'boolean ' => \ Magento \ Customer \ Block \ Adminhtml \ Form \ Element \ Boolean::class
137
+ 'file ' => File::class,
138
+ 'image ' => Image::class,
139
+ 'boolean ' => Boolean::class
123
140
];
124
141
}
125
142
126
143
/**
127
144
* Return array of additional form element renderers by element id
128
145
*
129
146
* @return array
147
+ * @throws LocalizedException
130
148
*/
131
149
protected function _getAdditionalFormElementRenderers ()
132
150
{
133
151
return [
134
152
'region ' => $ this ->getLayout ()->createBlock (
135
- \ Magento \ Customer \ Block \ Adminhtml \ Edit \ Renderer \ Region::class
153
+ Region::class
136
154
)
137
155
];
138
156
}
139
157
140
158
/**
141
159
* Add additional data to form element
142
160
*
143
- * @param \Magento\Framework\Data\Form\Element\ AbstractElement $element
161
+ * @param AbstractElement $element
144
162
* @return $this
145
163
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
146
164
*/
147
- protected function _addAdditionalFormElementData (\ Magento \ Framework \ Data \ Form \ Element \ AbstractElement $ element )
165
+ protected function _addAdditionalFormElementData (AbstractElement $ element )
148
166
{
149
167
return $ this ;
150
168
}
@@ -153,11 +171,12 @@ protected function _addAdditionalFormElementData(\Magento\Framework\Data\Form\El
153
171
* Add rendering EAV attributes to Form element
154
172
*
155
173
* @param AttributeMetadataInterface[] $attributes
156
- * @param \Magento\Framework\Data\ Form\AbstractForm $form
174
+ * @param Form\AbstractForm $form
157
175
* @return $this
158
176
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
177
+ * @throws LocalizedException
159
178
*/
160
- protected function _addAttributesToForm ($ attributes , \ Magento \ Framework \ Data \ Form \AbstractForm $ form )
179
+ protected function _addAttributesToForm ($ attributes , Form \AbstractForm $ form )
161
180
{
162
181
// add additional form types
163
182
$ types = $ this ->_getAdditionalFormElementTypes ();
@@ -178,40 +197,30 @@ protected function _addAttributesToForm($attributes, \Magento\Framework\Data\For
178
197
'label ' => __ ($ attribute ->getStoreLabel ()),
179
198
'class ' => $ this ->getValidationClasses ($ attribute ),
180
199
'required ' => $ attribute ->isRequired (),
200
+ 'sort_order ' => $ attribute ->getSortOrder ()
181
201
]
182
202
);
183
- if ($ inputType == 'multiline ' ) {
184
- $ element ->setLineCount ($ attribute ->getMultilineCount ());
203
+ switch ($ inputType ) {
204
+ case 'multiline ' :
205
+ $ element ->setLineCount ($ attribute ->getMultilineCount ());
206
+ break ;
207
+ case 'select ' :
208
+ case 'multiselect ' :
209
+ $ this ->addSelectOptions ($ attribute , $ element );
210
+ break ;
211
+ case 'date ' :
212
+ $ format = $ this ->_localeDate ->getDateFormat (
213
+ IntlDateFormatter::SHORT
214
+ );
215
+ $ element ->setDateFormat ($ format );
216
+ break ;
185
217
}
186
218
$ element ->setEntityAttribute ($ attribute );
187
219
$ this ->_addAdditionalFormElementData ($ element );
188
220
189
221
if (!empty ($ renderers [$ attribute ->getAttributeCode ()])) {
190
222
$ element ->setRenderer ($ renderers [$ attribute ->getAttributeCode ()]);
191
223
}
192
-
193
- if ($ inputType == 'select ' || $ inputType == 'multiselect ' ) {
194
- $ options = [];
195
- foreach ($ attribute ->getOptions () as $ optionData ) {
196
- $ data = $ this ->dataObjectProcessor ->buildOutputDataArray (
197
- $ optionData ,
198
- \Magento \Customer \Api \Data \OptionInterface::class
199
- );
200
- foreach ($ data as $ key => $ value ) {
201
- if (is_array ($ value )) {
202
- unset($ data [$ key ]);
203
- $ data ['value ' ] = $ value ;
204
- }
205
- }
206
- $ options [] = $ data ;
207
- }
208
- $ element ->setValues ($ options );
209
- } elseif ($ inputType == 'date ' ) {
210
- $ format = $ this ->_localeDate ->getDateFormat (
211
- \IntlDateFormatter::SHORT
212
- );
213
- $ element ->setDateFormat ($ format );
214
- }
215
224
}
216
225
}
217
226
@@ -245,8 +254,7 @@ private function getValidationClasses(AttributeMetadataInterface $attribute) : s
245
254
$ out = array_merge ($ out , $ textClasses );
246
255
}
247
256
248
- $ out = !empty ($ out ) ? implode (' ' , array_unique (array_filter ($ out ))) : '' ;
249
- return $ out ;
257
+ return implode (' ' , array_unique (array_filter ($ out )));
250
258
}
251
259
252
260
/**
@@ -281,4 +289,30 @@ private function getTextLengthValidateClasses(AttributeMetadataInterface $attrib
281
289
282
290
return $ classes ;
283
291
}
292
+
293
+ /**
294
+ * Add select options for SELECT and MULTISELECT attribute
295
+ *
296
+ * @param AttributeMetadataInterface $attribute
297
+ * @param AbstractElement $element
298
+ * @return void
299
+ */
300
+ private function addSelectOptions (AttributeMetadataInterface $ attribute , AbstractElement $ element ): void
301
+ {
302
+ $ options = [];
303
+ foreach ($ attribute ->getOptions () as $ optionData ) {
304
+ $ data = $ this ->dataObjectProcessor ->buildOutputDataArray (
305
+ $ optionData ,
306
+ OptionInterface::class
307
+ );
308
+ foreach ($ data as $ key => $ value ) {
309
+ if (is_array ($ value )) {
310
+ unset($ data [$ key ]);
311
+ $ data ['value ' ] = $ value ;
312
+ }
313
+ }
314
+ $ options [] = $ data ;
315
+ }
316
+ $ element ->setValues ($ options );
317
+ }
284
318
}
0 commit comments