55 */
66namespace Magento \Sales \Block \Adminhtml \Order \Create \Form ;
77
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 ;
822use Magento \Framework \Pricing \PriceCurrencyInterface ;
923use 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 ;
1027
1128/**
1229 * Sales Order Create Form Abstract Block
1330 *
1431 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1532 */
16- abstract class AbstractForm extends \ Magento \ Sales \ Block \ Adminhtml \ Order \ Create \ AbstractCreate
33+ abstract class AbstractForm extends AbstractCreate
1734{
1835 /**
19- * Form factory
20- *
21- * @var \Magento\Framework\Data\FormFactory
36+ * @var FormFactory
2237 */
2338 protected $ _formFactory ;
2439
2540 /**
2641 * Data Form object
2742 *
28- * @var \Magento\Framework\Data\ Form
43+ * @var Form
2944 */
3045 protected $ _form ;
3146
3247 /**
33- * @var \Magento\Framework\Reflection\ DataObjectProcessor
48+ * @var DataObjectProcessor
3449 */
3550 protected $ dataObjectProcessor ;
3651
3752 /**
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
4156 * @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
4459 * @param array $data
4560 */
4661 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 ,
5065 PriceCurrencyInterface $ priceCurrency ,
51- \ Magento \ Framework \ Data \ FormFactory $ formFactory ,
52- \ Magento \ Framework \ Reflection \ DataObjectProcessor $ dataObjectProcessor ,
66+ FormFactory $ formFactory ,
67+ DataObjectProcessor $ dataObjectProcessor ,
5368 array $ data = []
5469 ) {
5570 $ this ->_formFactory = $ formFactory ;
@@ -61,26 +76,27 @@ public function __construct(
6176 * Prepare global layout. Add renderers to \Magento\Framework\Data\Form
6277 *
6378 * @return $this
79+ * @throws LocalizedException
6480 */
6581 protected function _prepareLayout ()
6682 {
6783 parent ::_prepareLayout ();
6884
69- \ Magento \ Framework \ Data \ Form::setElementRenderer (
85+ Form::setElementRenderer (
7086 $ this ->getLayout ()->createBlock (
71- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Element::class,
87+ Element::class,
7288 $ this ->getNameInLayout () . '_element '
7389 )
7490 );
75- \ Magento \ Framework \ Data \ Form::setFieldsetRenderer (
91+ Form::setFieldsetRenderer (
7692 $ this ->getLayout ()->createBlock (
77- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset::class,
93+ Fieldset::class,
7894 $ this ->getNameInLayout () . '_fieldset '
7995 )
8096 );
81- \ Magento \ Framework \ Data \ Form::setFieldsetElementRenderer (
97+ Form::setFieldsetElementRenderer (
8298 $ this ->getLayout ()->createBlock (
83- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset \Element::class,
99+ Fieldset \Element::class,
84100 $ this ->getNameInLayout () . '_fieldset_element '
85101 )
86102 );
@@ -91,7 +107,8 @@ protected function _prepareLayout()
91107 /**
92108 * Return Form object
93109 *
94- * @return \Magento\Framework\Data\Form
110+ * @return Form
111+ * @throws LocalizedException
95112 */
96113 public function getForm ()
97114 {
@@ -117,34 +134,35 @@ abstract protected function _prepareForm();
117134 protected function _getAdditionalFormElementTypes ()
118135 {
119136 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
123140 ];
124141 }
125142
126143 /**
127144 * Return array of additional form element renderers by element id
128145 *
129146 * @return array
147+ * @throws LocalizedException
130148 */
131149 protected function _getAdditionalFormElementRenderers ()
132150 {
133151 return [
134152 'region ' => $ this ->getLayout ()->createBlock (
135- \ Magento \ Customer \ Block \ Adminhtml \ Edit \ Renderer \ Region::class
153+ Region::class
136154 )
137155 ];
138156 }
139157
140158 /**
141159 * Add additional data to form element
142160 *
143- * @param \Magento\Framework\Data\Form\Element\ AbstractElement $element
161+ * @param AbstractElement $element
144162 * @return $this
145163 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
146164 */
147- protected function _addAdditionalFormElementData (\ Magento \ Framework \ Data \ Form \ Element \ AbstractElement $ element )
165+ protected function _addAdditionalFormElementData (AbstractElement $ element )
148166 {
149167 return $ this ;
150168 }
@@ -153,11 +171,12 @@ protected function _addAdditionalFormElementData(\Magento\Framework\Data\Form\El
153171 * Add rendering EAV attributes to Form element
154172 *
155173 * @param AttributeMetadataInterface[] $attributes
156- * @param \Magento\Framework\Data\ Form\AbstractForm $form
174+ * @param Form\AbstractForm $form
157175 * @return $this
158176 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
177+ * @throws LocalizedException
159178 */
160- protected function _addAttributesToForm ($ attributes , \ Magento \ Framework \ Data \ Form \AbstractForm $ form )
179+ protected function _addAttributesToForm ($ attributes , Form \AbstractForm $ form )
161180 {
162181 // add additional form types
163182 $ types = $ this ->_getAdditionalFormElementTypes ();
@@ -178,40 +197,30 @@ protected function _addAttributesToForm($attributes, \Magento\Framework\Data\For
178197 'label ' => __ ($ attribute ->getStoreLabel ()),
179198 'class ' => $ this ->getValidationClasses ($ attribute ),
180199 'required ' => $ attribute ->isRequired (),
200+ 'sort_order ' => $ attribute ->getSortOrder ()
181201 ]
182202 );
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 ;
185217 }
186218 $ element ->setEntityAttribute ($ attribute );
187219 $ this ->_addAdditionalFormElementData ($ element );
188220
189221 if (!empty ($ renderers [$ attribute ->getAttributeCode ()])) {
190222 $ element ->setRenderer ($ renderers [$ attribute ->getAttributeCode ()]);
191223 }
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- }
215224 }
216225 }
217226
@@ -245,8 +254,7 @@ private function getValidationClasses(AttributeMetadataInterface $attribute) : s
245254 $ out = array_merge ($ out , $ textClasses );
246255 }
247256
248- $ out = !empty ($ out ) ? implode (' ' , array_unique (array_filter ($ out ))) : '' ;
249- return $ out ;
257+ return implode (' ' , array_unique (array_filter ($ out )));
250258 }
251259
252260 /**
@@ -281,4 +289,30 @@ private function getTextLengthValidateClasses(AttributeMetadataInterface $attrib
281289
282290 return $ classes ;
283291 }
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+ }
284318}
0 commit comments