-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathForm.php
286 lines (261 loc) · 7.74 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Block\Widget;
use Magento\Framework\App\ObjectManager;
/**
* Backend form widget
*
* @api
* @deprecated 100.2.0 in favour of UI component implementation
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @since 100.0.2
*/
class Form extends \Magento\Backend\Block\Widget
{
/**
* Form Object
*
* @var \Magento\Framework\Data\Form
*/
protected $_form;
/**
* @var string
*/
protected $_template = 'Magento_Backend::widget/form.phtml';
/** @var Form\Element\ElementCreator */
private $creator;
/**
* Constructs form
*
* @param \Magento\Backend\Block\Template\Context $context
* @param array $data
* @param Form\Element\ElementCreator|null $creator
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = [],
Form\Element\ElementCreator $creator = null
) {
parent::__construct($context, $data);
$this->creator = $creator ?: ObjectManager::getInstance()->get(Form\Element\ElementCreator::class);
}
/**
* Class constructor
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setDestElementId('edit_form');
}
/**
* Preparing global layout
*
* You can redefine this method in child classes for changing layout
*
* @return $this
*/
protected function _prepareLayout()
{
\Magento\Framework\Data\Form::setElementRenderer(
$this->getLayout()->createBlock(
\Magento\Backend\Block\Widget\Form\Renderer\Element::class,
$this->getNameInLayout() . '_element'
)
);
\Magento\Framework\Data\Form::setFieldsetRenderer(
$this->getLayout()->createBlock(
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset::class,
$this->getNameInLayout() . '_fieldset'
)
);
\Magento\Framework\Data\Form::setFieldsetElementRenderer(
$this->getLayout()->createBlock(
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset\Element::class,
$this->getNameInLayout() . '_fieldset_element'
)
);
return parent::_prepareLayout();
}
/**
* Get form object
*
* @return \Magento\Framework\Data\Form
*/
public function getForm()
{
return $this->_form;
}
/**
* Get form HTML
*
* @return string
*/
public function getFormHtml()
{
if (is_object($this->getForm())) {
return $this->getForm()->getHtml();
}
return '';
}
/**
* Set form object
*
* @param \Magento\Framework\Data\Form $form
* @return $this
*/
public function setForm(\Magento\Framework\Data\Form $form)
{
$this->_form = $form;
$this->_form->setParent($this);
$this->_form->setBaseUrl($this->_urlBuilder->getBaseUrl());
$customAttributes = $this->getData('custom_attributes');
if (is_array($customAttributes)) {
foreach ($customAttributes as $key => $value) {
$this->_form->addCustomAttribute($key, $value);
}
}
return $this;
}
/**
* Prepare form before rendering HTML
*
* @return $this
*/
protected function _prepareForm()
{
return $this;
}
/**
* This method is called before rendering HTML
*
* @return $this
*/
protected function _beforeToHtml()
{
$this->_prepareForm();
$this->_initFormValues();
return parent::_beforeToHtml();
}
/**
* Initialize form fields values
*
* Method will be called after prepareForm and can be used for field values initialization
*
* @return $this
*/
protected function _initFormValues()
{
return $this;
}
/**
* Set Fieldset to Form
*
* @param array $attributes attributes that are to be added
* @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset
* @param array $exclude attributes that should be skipped
* @return void
*/
protected function _setFieldset($attributes, $fieldset, $exclude = [])
{
$this->_addElementTypes($fieldset);
foreach ($attributes as $attribute) {
/* @var $attribute \Magento\Eav\Model\Entity\Attribute */
if (!$this->_isAttributeVisible($attribute)) {
continue;
}
if (($inputType = $attribute->getFrontend()->getInputType())
&& !in_array($attribute->getAttributeCode(), $exclude)
&& ('media_image' !== $inputType || $attribute->getAttributeCode() == 'image')
) {
$element = $this->creator->create($fieldset, $attribute);
$element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
$this->_applyTypeSpecificConfig($inputType, $element, $attribute);
}
}
}
/**
* Check whether attribute is visible
*
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @return bool
*/
protected function _isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attribute)
{
return !(!$attribute || $attribute->hasIsVisible() && !$attribute->getIsVisible());
}
/**
* Apply configuration specific for different element type
*
* @param string $inputType
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @return void
*/
protected function _applyTypeSpecificConfig($inputType, $element, \Magento\Eav\Model\Entity\Attribute $attribute)
{
switch ($inputType) {
case 'select':
$element->setValues($attribute->getSource()->getAllOptions(true, true));
break;
case 'multiselect':
$element->setValues($attribute->getSource()->getAllOptions(false, true));
$element->setCanBeEmpty(true);
break;
case 'date':
$element->setDateFormat($this->_localeDate->getDateFormatWithLongYear());
break;
case 'datetime':
$element->setDateFormat($this->_localeDate->getDateFormatWithLongYear());
$element->setTimeFormat($this->_localeDate->getTimeFormat());
break;
case 'multiline':
$element->setLineCount($attribute->getMultilineCount());
break;
default:
break;
}
}
/**
* Add new element type
*
* @param \Magento\Framework\Data\Form\AbstractForm $baseElement
* @return void
*/
protected function _addElementTypes(\Magento\Framework\Data\Form\AbstractForm $baseElement)
{
$types = array_merge(
[
'datetime' => 'date'
],
$this->_getAdditionalElementTypes()
);
foreach ($types as $code => $className) {
$baseElement->addType($code, $className);
}
}
/**
* Retrieve predefined additional element types
*
* @return array
*/
protected function _getAdditionalElementTypes()
{
return [];
}
/**
* Render additional element
*
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function _getAdditionalElementHtml($element)
{
return '';
}
}