Skip to content

Commit fdd599c

Browse files
authored
Merge pull request #3313 from magento-tsg-csl3/2.2-develop-pr5
[TSG-CSL3] Backporting 2.2 (pr5)
2 parents 693c009 + bfce815 commit fdd599c

File tree

28 files changed

+852
-597
lines changed

28 files changed

+852
-597
lines changed

app/code/Magento/Backend/Block/Widget/Form.php

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace 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
*/
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Backend\Block\Widget\Form\Element;
8+
9+
use Magento\Eav\Model\Entity\Attribute;
10+
use Magento\Framework\Data\Form\Element\AbstractElement;
11+
use Magento\Framework\Data\Form\Element\Fieldset;
12+
13+
/**
14+
* Class ElementCreator
15+
*
16+
* @deprecated 100.2.7 in favour of UI component implementation
17+
*/
18+
class ElementCreator
19+
{
20+
/**
21+
* @var array
22+
*/
23+
private $modifiers;
24+
25+
/**
26+
* ElementCreator constructor.
27+
*
28+
* @param array $modifiers
29+
*/
30+
public function __construct(array $modifiers = [])
31+
{
32+
$this->modifiers = $modifiers;
33+
}
34+
35+
/**
36+
* Creates element
37+
*
38+
* @param Fieldset $fieldset
39+
* @param Attribute $attribute
40+
*
41+
* @return AbstractElement
42+
*/
43+
public function create(Fieldset $fieldset, Attribute $attribute): AbstractElement
44+
{
45+
$config = $this->getElementConfig($attribute);
46+
47+
if (!empty($config['rendererClass'])) {
48+
$fieldType = $config['inputType'] . '_' . $attribute->getAttributeCode();
49+
$fieldset->addType($fieldType, $config['rendererClass']);
50+
}
51+
52+
return $fieldset
53+
->addField($config['attribute_code'], $config['inputType'], $config)
54+
->setEntityAttribute($attribute);
55+
}
56+
57+
/**
58+
* Returns element config
59+
*
60+
* @param Attribute $attribute
61+
* @return array
62+
*/
63+
private function getElementConfig(Attribute $attribute): array
64+
{
65+
$defaultConfig = $this->createDefaultConfig($attribute);
66+
$config = $this->modifyConfig($defaultConfig);
67+
68+
$config['label'] = __($config['label']);
69+
70+
return $config;
71+
}
72+
73+
/**
74+
* Returns default config
75+
*
76+
* @param Attribute $attribute
77+
* @return array
78+
*/
79+
private function createDefaultConfig(Attribute $attribute): array
80+
{
81+
return [
82+
'inputType' => $attribute->getFrontend()->getInputType(),
83+
'rendererClass' => $attribute->getFrontend()->getInputRendererClass(),
84+
'attribute_code' => $attribute->getAttributeCode(),
85+
'name' => $attribute->getAttributeCode(),
86+
'label' => $attribute->getFrontend()->getLabel(),
87+
'class' => $attribute->getFrontend()->getClass(),
88+
'required' => $attribute->getIsRequired(),
89+
'note' => $attribute->getNote(),
90+
];
91+
}
92+
93+
/**
94+
* Modify config
95+
*
96+
* @param array $config
97+
* @return array
98+
*/
99+
private function modifyConfig(array $config): array
100+
{
101+
if ($this->isModified($config['attribute_code'])) {
102+
return $this->applyModifier($config);
103+
}
104+
return $config;
105+
}
106+
107+
/**
108+
* Returns bool if attribute need to modify
109+
*
110+
* @param string $attribute_code
111+
* @return bool
112+
*/
113+
private function isModified($attribute_code): bool
114+
{
115+
return isset($this->modifiers[$attribute_code]);
116+
}
117+
118+
/**
119+
* Apply modifier to config
120+
*
121+
* @param array $config
122+
* @return array
123+
*/
124+
private function applyModifier(array $config): array
125+
{
126+
$modifiedConfig = $this->modifiers[$config['attribute_code']];
127+
foreach (array_keys($config) as $key) {
128+
if (isset($modifiedConfig[$key])) {
129+
$config[$key] = $modifiedConfig[$key];
130+
}
131+
}
132+
return $config;
133+
}
134+
}

0 commit comments

Comments
 (0)