33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Backend \Block \Widget ;
78
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+
820/**
921 * Backend form widget
1022 *
@@ -18,7 +30,7 @@ class Form extends \Magento\Backend\Block\Widget
1830 /**
1931 * Form Object
2032 *
21- * @var \Magento\Framework\Data\Form
33+ * @var DataForm
2234 */
2335 protected $ _form ;
2436
@@ -28,12 +40,24 @@ class Form extends \Magento\Backend\Block\Widget
2840 protected $ _template = 'Magento_Backend::widget/form.phtml ' ;
2941
3042 /**
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
3251 * @param array $data
52+ * @param ElementCreator|null $creator
3353 */
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+ ) {
3659 parent ::__construct ($ context , $ data );
60+ $ this ->creator = $ creator ?: ObjectManager::getInstance ()->get (ElementCreator::class);
3761 }
3862
3963 /**
@@ -58,21 +82,21 @@ protected function _construct()
5882 */
5983 protected function _prepareLayout ()
6084 {
61- \ Magento \ Framework \ Data \Form ::setElementRenderer (
85+ DataForm ::setElementRenderer (
6286 $ this ->getLayout ()->createBlock (
63- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Element::class,
87+ Element::class,
6488 $ this ->getNameInLayout () . '_element '
6589 )
6690 );
67- \ Magento \ Framework \ Data \Form ::setFieldsetRenderer (
91+ DataForm ::setFieldsetRenderer (
6892 $ this ->getLayout ()->createBlock (
69- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset::class,
93+ Fieldset::class,
7094 $ this ->getNameInLayout () . '_fieldset '
7195 )
7296 );
73- \ Magento \ Framework \ Data \Form ::setFieldsetElementRenderer (
97+ DataForm ::setFieldsetElementRenderer (
7498 $ this ->getLayout ()->createBlock (
75- \ Magento \ Backend \ Block \ Widget \ Form \ Renderer \ Fieldset \Element ::class,
99+ FieldsetElement ::class,
76100 $ this ->getNameInLayout () . '_fieldset_element '
77101 )
78102 );
@@ -83,7 +107,7 @@ protected function _prepareLayout()
83107 /**
84108 * Get form object
85109 *
86- * @return \Magento\Framework\Data\Form
110+ * @return DataForm
87111 */
88112 public function getForm ()
89113 {
@@ -106,10 +130,10 @@ public function getFormHtml()
106130 /**
107131 * Set form object
108132 *
109- * @param \Magento\Framework\Data\Form $form
133+ * @param DataForm $form
110134 * @return $this
111135 */
112- public function setForm (\ Magento \ Framework \ Data \ Form $ form )
136+ public function setForm (DataForm $ form )
113137 {
114138 $ this ->_form = $ form ;
115139 $ this ->_form ->setParent ($ this );
@@ -148,6 +172,7 @@ protected function _beforeToHtml()
148172
149173 /**
150174 * Initialize form fields values
175+ *
151176 * Method will be called after prepareForm and can be used for field values initialization
152177 *
153178 * @return $this
@@ -169,36 +194,15 @@ protected function _setFieldset($attributes, $fieldset, $exclude = [])
169194 {
170195 $ this ->_addElementTypes ($ fieldset );
171196 foreach ($ attributes as $ attribute ) {
172- /* @var $attribute \Magento\Eav\Model\Entity\ Attribute */
197+ /* @var $attribute Attribute */
173198 if (!$ this ->_isAttributeVisible ($ attribute )) {
174199 continue ;
175200 }
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 ' )
180204 ) {
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 );
202206 $ element ->setAfterElementHtml ($ this ->_getAdditionalElementHtml ($ element ));
203207
204208 $ this ->_applyTypeSpecificConfig ($ inputType , $ element , $ attribute );
@@ -209,10 +213,10 @@ protected function _setFieldset($attributes, $fieldset, $exclude = [])
209213 /**
210214 * Check whether attribute is visible
211215 *
212- * @param \Magento\Eav\Model\Entity\ Attribute $attribute
216+ * @param Attribute $attribute
213217 * @return bool
214218 */
215- protected function _isAttributeVisible (\ Magento \ Eav \ Model \ Entity \ Attribute $ attribute )
219+ protected function _isAttributeVisible (Attribute $ attribute )
216220 {
217221 return !(!$ attribute || $ attribute ->hasIsVisible () && !$ attribute ->getIsVisible ());
218222 }
@@ -221,11 +225,11 @@ protected function _isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attr
221225 * Apply configuration specific for different element type
222226 *
223227 * @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
226230 * @return void
227231 */
228- protected function _applyTypeSpecificConfig ($ inputType , $ element , \ Magento \ Eav \ Model \ Entity \ Attribute $ attribute )
232+ protected function _applyTypeSpecificConfig ($ inputType , $ element , Attribute $ attribute )
229233 {
230234 switch ($ inputType ) {
231235 case 'select ' :
@@ -249,10 +253,10 @@ protected function _applyTypeSpecificConfig($inputType, $element, \Magento\Eav\M
249253 /**
250254 * Add new element type
251255 *
252- * @param \Magento\Framework\Data\Form\ AbstractForm $baseElement
256+ * @param AbstractForm $baseElement
253257 * @return void
254258 */
255- protected function _addElementTypes (\ Magento \ Framework \ Data \ Form \ AbstractForm $ baseElement )
259+ protected function _addElementTypes (AbstractForm $ baseElement )
256260 {
257261 $ types = $ this ->_getAdditionalElementTypes ();
258262 foreach ($ types as $ code => $ className ) {
@@ -273,7 +277,7 @@ protected function _getAdditionalElementTypes()
273277 /**
274278 * Render additional element
275279 *
276- * @param \Magento\Framework\Data\Form\Element\ AbstractElement $element
280+ * @param AbstractElement $element
277281 * @return string
278282 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
279283 */
0 commit comments