Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Form Class Reference
Inheritance diagram for Form:
Widget Template Template AbstractBlock DataObject BlockInterface Generic AttributeSet Currencysymbol AbstractTab Edit Form Form General Form AbstractForm Additional DefaultAdditional Form Content Form Advanced Front System Formattribute Formgroup Formset Filter NewCategory Price Conditions Form Form Form Form GenericMetadata Newsletter Form AbstractMain Form Form Form Form Form Webapi Form Info Webapi Tokens Form Form Form Form Form NewVideo Form Form Form Form Form Form Form Form Actions Conditions Form Labels Form Form Form Form AbstractTab Form Form Info Form Main Form Form Form Main Settings Options Googleoptimizer Googleoptimizer

Public Member Functions

 __construct (\Magento\Backend\Block\Template\Context $context, array $data=[])
 
 getForm ()
 
 getFormHtml ()
 
 setForm (\Magento\Framework\Data\Form $form)
 
- Public Member Functions inherited from Widget
 getId ()
 
 getSuffixId ($suffix)
 
 getHtmlId ()
 
 getCurrentUrl ($params=[])
 
 getButtonHtml ($label, $onclick, $class='', $buttonId=null, $dataAttr=[])
 
- Public Member Functions inherited from Template
 __construct (\Magento\Backend\Block\Template\Context $context, array $data=[])
 
 getFormKey ()
 
 isOutputEnabled ($moduleName=null)
 
 getAuthorization ()
 
 getToolbar ()
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Member Functions

 _construct ()
 
 _prepareLayout ()
 
 _prepareForm ()
 
 _beforeToHtml ()
 
 _initFormValues ()
 
 _setFieldset ($attributes, $fieldset, $exclude=[])
 
 _isAttributeVisible (\Magento\Eav\Model\Entity\Attribute $attribute)
 
 _applyTypeSpecificConfig ($inputType, $element, \Magento\Eav\Model\Entity\Attribute $attribute)
 
 _addElementTypes (\Magento\Framework\Data\Form\AbstractForm $baseElement)
 
 _getAdditionalElementTypes ()
 
 _getAdditionalElementHtml ($element)
 
- Protected Member Functions inherited from Widget
 _addBreadcrumb ($label, $title=null, $link=null)
 
- Protected Member Functions inherited from Template
 _toHtml ()
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_form
 
 $_template = 'Magento_Backend::widget/form.phtml'
 
- Protected Attributes inherited from Template
 $_authorization
 
 $mathRandom
 
 $_backendSession
 
 $formKey
 
 $nameBuilder
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Backend form widget

@api

Deprecated:
100.2.0 in favour of UI component implementation @SuppressWarnings(PHPMD.NumberOfChildren)
Since
100.0.2

Definition at line 16 of file Form.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Backend\Block\Template\Context  $context,
array  $data = [] 
)
Parameters
\Magento\Backend\Block\Template\Context$context
array$data

Definition at line 34 of file Form.php.

35  {
36  parent::__construct($context, $data);
37  }

Member Function Documentation

◆ _addElementTypes()

_addElementTypes ( \Magento\Framework\Data\Form\AbstractForm  $baseElement)
protected

Add new element type

Parameters
\Magento\Framework\Data\Form\AbstractForm$baseElement
Returns
void

Definition at line 255 of file Form.php.

256  {
257  $types = $this->_getAdditionalElementTypes();
258  foreach ($types as $code => $className) {
259  $baseElement->addType($code, $className);
260  }
261  }
$code
Definition: info.phtml:12
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ _applyTypeSpecificConfig()

_applyTypeSpecificConfig (   $inputType,
  $element,
\Magento\Eav\Model\Entity\Attribute  $attribute 
)
protected

Apply configuration specific for different element type

Parameters
string$inputType
\Magento\Framework\Data\Form\Element\AbstractElement$element
\Magento\Eav\Model\Entity\Attribute$attribute
Returns
void

Definition at line 228 of file Form.php.

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  }
$element
Definition: element.phtml:12

◆ _beforeToHtml()

_beforeToHtml ( )
protected

This method is called before rendering HTML

Returns
$this

Definition at line 142 of file Form.php.

143  {
144  $this->_prepareForm();
145  $this->_initFormValues();
146  return parent::_beforeToHtml();
147  }

◆ _construct()

_construct ( )
protected

Class constructor

Returns
void

Definition at line 44 of file Form.php.

45  {
46  parent::_construct();
47 
48  $this->setDestElementId('edit_form');
49  $this->setShowGlobalIcon(false);
50  }

◆ _getAdditionalElementHtml()

_getAdditionalElementHtml (   $element)
protected

Render additional element

Parameters
\Magento\Framework\Data\Form\Element\AbstractElement$element
Returns
string @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 280 of file Form.php.

281  {
282  return '';
283  }

◆ _getAdditionalElementTypes()

_getAdditionalElementTypes ( )
protected

Retrieve predefined additional element types

Returns
array

Definition at line 268 of file Form.php.

269  {
270  return [];
271  }

◆ _initFormValues()

_initFormValues ( )
protected

Initialize form fields values Method will be called after prepareForm and can be used for field values initialization

Returns
$this

Definition at line 155 of file Form.php.

156  {
157  return $this;
158  }

◆ _isAttributeVisible()

_isAttributeVisible ( \Magento\Eav\Model\Entity\Attribute  $attribute)
protected

Check whether attribute is visible

Parameters
\Magento\Eav\Model\Entity\Attribute$attribute
Returns
bool

Definition at line 215 of file Form.php.

216  {
217  return !(!$attribute || $attribute->hasIsVisible() && !$attribute->getIsVisible());
218  }

◆ _prepareForm()

_prepareForm ( )
protected

Prepare form before rendering HTML

Returns
$this

Definition at line 132 of file Form.php.

133  {
134  return $this;
135  }

◆ _prepareLayout()

_prepareLayout ( )
protected

Preparing global layout

You can redefine this method in child classes for changing layout

Returns
$this

Definition at line 59 of file Form.php.

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  }
static setFieldsetElementRenderer(RendererInterface $renderer=null)
Definition: Form.php:96
static setElementRenderer(RendererInterface $renderer=null)
Definition: Form.php:78
static setFieldsetRenderer(RendererInterface $renderer=null)
Definition: Form.php:87

◆ _setFieldset()

_setFieldset (   $attributes,
  $fieldset,
  $exclude = [] 
)
protected

Set Fieldset to Form

Parameters
array$attributesattributes that are to be added
\Magento\Framework\Data\Form\Element\Fieldset$fieldset
array$excludeattributes that should be skipped
Returns
void

Definition at line 168 of file Form.php.

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  }
_addElementTypes(\Magento\Framework\Data\Form\AbstractForm $baseElement)
Definition: Form.php:255
_isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Form.php:215
_applyTypeSpecificConfig($inputType, $element, \Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Form.php:228
$attributes
Definition: matrix.phtml:13
$element
Definition: element.phtml:12

◆ getForm()

getForm ( )

Get form object

Returns
\Magento\Framework\Data\Form

Definition at line 88 of file Form.php.

89  {
90  return $this->_form;
91  }

◆ getFormHtml()

getFormHtml ( )

Get form HTML

Returns
string

Definition at line 98 of file Form.php.

99  {
100  if (is_object($this->getForm())) {
101  return $this->getForm()->getHtml();
102  }
103  return '';
104  }

◆ setForm()

setForm ( \Magento\Framework\Data\Form  $form)

Set form object

Parameters
\Magento\Framework\Data\Form$form
Returns
$this

Definition at line 112 of file Form.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
$value
Definition: gender.phtml:16

Field Documentation

◆ $_form

$_form
protected

Definition at line 23 of file Form.php.

◆ $_template

$_template = 'Magento_Backend::widget/form.phtml'
protected

Definition at line 28 of file Form.php.


The documentation for this class was generated from the following file: