Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Form.php
Go to the documentation of this file.
1 <?php
7 
17 {
23  protected $_form;
24 
28  protected $_template = 'Magento_Backend::widget/form.phtml';
29 
34  public function __construct(\Magento\Backend\Block\Template\Context $context, array $data = [])
35  {
36  parent::__construct($context, $data);
37  }
38 
44  protected function _construct()
45  {
46  parent::_construct();
47 
48  $this->setDestElementId('edit_form');
49  $this->setShowGlobalIcon(false);
50  }
51 
59  protected function _prepareLayout()
60  {
62  $this->getLayout()->createBlock(
63  \Magento\Backend\Block\Widget\Form\Renderer\Element::class,
64  $this->getNameInLayout() . '_element'
65  )
66  );
68  $this->getLayout()->createBlock(
69  \Magento\Backend\Block\Widget\Form\Renderer\Fieldset::class,
70  $this->getNameInLayout() . '_fieldset'
71  )
72  );
74  $this->getLayout()->createBlock(
75  \Magento\Backend\Block\Widget\Form\Renderer\Fieldset\Element::class,
76  $this->getNameInLayout() . '_fieldset_element'
77  )
78  );
79 
80  return parent::_prepareLayout();
81  }
82 
88  public function getForm()
89  {
90  return $this->_form;
91  }
92 
98  public function getFormHtml()
99  {
100  if (is_object($this->getForm())) {
101  return $this->getForm()->getHtml();
102  }
103  return '';
104  }
105 
112  public function setForm(\Magento\Framework\Data\Form $form)
113  {
114  $this->_form = $form;
115  $this->_form->setParent($this);
116  $this->_form->setBaseUrl($this->_urlBuilder->getBaseUrl());
117 
118  $customAttributes = $this->getData('custom_attributes');
119  if (is_array($customAttributes)) {
120  foreach ($customAttributes as $key => $value) {
121  $this->_form->addCustomAttribute($key, $value);
122  }
123  }
124  return $this;
125  }
126 
132  protected function _prepareForm()
133  {
134  return $this;
135  }
136 
142  protected function _beforeToHtml()
143  {
144  $this->_prepareForm();
145  $this->_initFormValues();
146  return parent::_beforeToHtml();
147  }
148 
155  protected function _initFormValues()
156  {
157  return $this;
158  }
159 
168  protected function _setFieldset($attributes, $fieldset, $exclude = [])
169  {
170  $this->_addElementTypes($fieldset);
171  foreach ($attributes as $attribute) {
172  /* @var $attribute \Magento\Eav\Model\Entity\Attribute */
173  if (!$this->_isAttributeVisible($attribute)) {
174  continue;
175  }
176  if (($inputType = $attribute->getFrontend()->getInputType()) && !in_array(
177  $attribute->getAttributeCode(),
178  $exclude
179  ) && ('media_image' != $inputType || $attribute->getAttributeCode() == 'image')
180  ) {
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 
202  $element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
203 
204  $this->_applyTypeSpecificConfig($inputType, $element, $attribute);
205  }
206  }
207  }
208 
215  protected function _isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attribute)
216  {
217  return !(!$attribute || $attribute->hasIsVisible() && !$attribute->getIsVisible());
218  }
219 
228  protected function _applyTypeSpecificConfig($inputType, $element, \Magento\Eav\Model\Entity\Attribute $attribute)
229  {
230  switch ($inputType) {
231  case 'select':
232  $element->setValues($attribute->getSource()->getAllOptions(true, true));
233  break;
234  case 'multiselect':
235  $element->setValues($attribute->getSource()->getAllOptions(false, true));
236  $element->setCanBeEmpty(true);
237  break;
238  case 'date':
239  $element->setDateFormat($this->_localeDate->getDateFormatWithLongYear());
240  break;
241  case 'multiline':
242  $element->setLineCount($attribute->getMultilineCount());
243  break;
244  default:
245  break;
246  }
247  }
248 
255  protected function _addElementTypes(\Magento\Framework\Data\Form\AbstractForm $baseElement)
256  {
257  $types = $this->_getAdditionalElementTypes();
258  foreach ($types as $code => $className) {
259  $baseElement->addType($code, $className);
260  }
261  }
262 
268  protected function _getAdditionalElementTypes()
269  {
270  return [];
271  }
272 
281  {
282  return '';
283  }
284 }
_addElementTypes(\Magento\Framework\Data\Form\AbstractForm $baseElement)
Definition: Form.php:255
getData($key='', $index=null)
Definition: DataObject.php:119
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
_isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Form.php:215
__construct(\Magento\Backend\Block\Template\Context $context, array $data=[])
Definition: Form.php:34
static setFieldsetElementRenderer(RendererInterface $renderer=null)
Definition: Form.php:96
static setElementRenderer(RendererInterface $renderer=null)
Definition: Form.php:78
$value
Definition: gender.phtml:16
_setFieldset($attributes, $fieldset, $exclude=[])
Definition: Form.php:168
_applyTypeSpecificConfig($inputType, $element, \Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Form.php:228
$attributes
Definition: matrix.phtml:13
static setFieldsetRenderer(RendererInterface $renderer=null)
Definition: Form.php:87
$code
Definition: info.phtml:12
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
$element
Definition: element.phtml:12