Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 171c653

Browse files
committed
Merge branch '2.3-develop' of github.com:magento/magento2ce into MAGETWO-91388
2 parents 6680740 + de50d12 commit 171c653

40 files changed

+1731
-499
lines changed

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
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\Framework\App\ObjectManager;
10+
811
/**
912
* Backend form widget
1013
*
@@ -27,13 +30,23 @@ class Form extends \Magento\Backend\Block\Widget
2730
*/
2831
protected $_template = 'Magento_Backend::widget/form.phtml';
2932

33+
/** @var Form\Element\ElementCreator */
34+
private $creator;
35+
3036
/**
37+
* Constructs form
38+
*
3139
* @param \Magento\Backend\Block\Template\Context $context
3240
* @param array $data
41+
* @param Form\Element\ElementCreator|null $creator
3342
*/
34-
public function __construct(\Magento\Backend\Block\Template\Context $context, array $data = [])
35-
{
43+
public function __construct(
44+
\Magento\Backend\Block\Template\Context $context,
45+
array $data = [],
46+
Form\Element\ElementCreator $creator = null
47+
) {
3648
parent::__construct($context, $data);
49+
$this->creator = $creator ?: ObjectManager::getInstance()->get(Form\Element\ElementCreator::class);
3750
}
3851

3952
/**
@@ -148,6 +161,7 @@ protected function _beforeToHtml()
148161

149162
/**
150163
* Initialize form fields values
164+
*
151165
* Method will be called after prepareForm and can be used for field values initialization
152166
*
153167
* @return $this
@@ -173,32 +187,11 @@ protected function _setFieldset($attributes, $fieldset, $exclude = [])
173187
if (!$this->_isAttributeVisible($attribute)) {
174188
continue;
175189
}
176-
if (($inputType = $attribute->getFrontend()->getInputType()) && !in_array(
177-
$attribute->getAttributeCode(),
178-
$exclude
179-
) && ('media_image' != $inputType || $attribute->getAttributeCode() == 'image')
190+
if (($inputType = $attribute->getFrontend()->getInputType())
191+
&& !in_array($attribute->getAttributeCode(), $exclude)
192+
&& ('media_image' !== $inputType || $attribute->getAttributeCode() == 'image')
180193
) {
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-
194+
$element = $this->creator->create($fieldset, $attribute);
202195
$element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
203196

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

app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Action/Attribute/Tab/Attributes.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
*
1010
* @author Magento Core Team <[email protected]>
1111
*/
12+
declare(strict_types=1);
13+
1214
namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab;
1315

1416
use Magento\Framework\Data\Form\Element\AbstractElement;
1517

1618
/**
19+
* Attributes tab block
20+
*
1721
* @api
1822
* @SuppressWarnings(PHPMD.DepthOfInheritance)
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1924
* @since 100.0.2
2025
*/
2126
class Attributes extends \Magento\Catalog\Block\Adminhtml\Form implements
@@ -31,28 +36,37 @@ class Attributes extends \Magento\Catalog\Block\Adminhtml\Form implements
3136
*/
3237
protected $_attributeAction;
3338

39+
/** @var array */
40+
private $excludeFields;
41+
3442
/**
3543
* @param \Magento\Backend\Block\Template\Context $context
3644
* @param \Magento\Framework\Registry $registry
3745
* @param \Magento\Framework\Data\FormFactory $formFactory
3846
* @param \Magento\Catalog\Model\ProductFactory $productFactory
3947
* @param \Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeAction
4048
* @param array $data
49+
* @param array|null $excludeFields
4150
*/
4251
public function __construct(
4352
\Magento\Backend\Block\Template\Context $context,
4453
\Magento\Framework\Registry $registry,
4554
\Magento\Framework\Data\FormFactory $formFactory,
4655
\Magento\Catalog\Model\ProductFactory $productFactory,
4756
\Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeAction,
48-
array $data = []
57+
array $data = [],
58+
array $excludeFields = null
4959
) {
5060
$this->_attributeAction = $attributeAction;
5161
$this->_productFactory = $productFactory;
62+
$this->excludeFields = $excludeFields ?: [];
63+
5264
parent::__construct($context, $registry, $formFactory, $data);
5365
}
5466

5567
/**
68+
* Construct block
69+
*
5670
* @return void
5771
*/
5872
protected function _construct()
@@ -62,20 +76,14 @@ protected function _construct()
6276
}
6377

6478
/**
79+
* Prepares form
80+
*
6581
* @return void
82+
* @throws \Magento\Framework\Exception\LocalizedException
6683
*/
67-
protected function _prepareForm()
84+
protected function _prepareForm(): void
6885
{
69-
$this->setFormExcludedFieldList(
70-
[
71-
'category_ids',
72-
'gallery',
73-
'image',
74-
'media_gallery',
75-
'quantity_and_stock_status',
76-
'tier_price',
77-
]
78-
);
86+
$this->setFormExcludedFieldList($this->getExcludedFields());
7987
$this->_eventManager->dispatch(
8088
'adminhtml_catalog_product_form_prepare_excluded_field_list',
8189
['object' => $this]
@@ -149,12 +157,14 @@ protected function _getAdditionalElementHtml($element)
149157
weightHandle.hideWeightSwitcher();
150158
});</script>
151159
HTML;
152-
// @codingStandardsIgnoreEnd
160+
// @codingStandardsIgnoreEnd
153161
}
154162
return $html;
155163
}
156164

157165
/**
166+
* Returns tab label
167+
*
158168
* @return \Magento\Framework\Phrase
159169
*/
160170
public function getTabLabel()
@@ -163,6 +173,8 @@ public function getTabLabel()
163173
}
164174

165175
/**
176+
* Return Tab title
177+
*
166178
* @return \Magento\Framework\Phrase
167179
*/
168180
public function getTabTitle()
@@ -171,6 +183,8 @@ public function getTabTitle()
171183
}
172184

173185
/**
186+
* Can show tab in tabs
187+
*
174188
* @return bool
175189
*/
176190
public function canShowTab()
@@ -179,10 +193,22 @@ public function canShowTab()
179193
}
180194

181195
/**
196+
* Tab not hidden
197+
*
182198
* @return bool
183199
*/
184200
public function isHidden()
185201
{
186202
return false;
187203
}
204+
205+
/**
206+
* Returns excluded fields
207+
*
208+
* @return array
209+
*/
210+
private function getExcludedFields(): array
211+
{
212+
return $this->excludeFields;
213+
}
188214
}

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,16 @@
220220
<argument name="filter" xsi:type="object">Magento\Catalog\Ui\DataProvider\Product\AddSearchKeyConditionToCollection</argument>
221221
</arguments>
222222
</type>
223+
<type name="Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab\Attributes">
224+
<arguments>
225+
<argument name="excludeFields" xsi:type="array">
226+
<item name="0" xsi:type="string">category_ids</item>
227+
<item name="1" xsi:type="string">gallery</item>
228+
<item name="2" xsi:type="string">image</item>
229+
<item name="3" xsi:type="string">media_gallery</item>
230+
<item name="4" xsi:type="string">quantity_and_stock_status</item>
231+
<item name="5" xsi:type="string">tier_price</item>
232+
</argument>
233+
</arguments>
234+
</type>
223235
</config>

0 commit comments

Comments
 (0)