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:
Form Form

Public Member Functions

 __construct (\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\Module\Dir\Reader $modulesReader, \Magento\Eav\Model\AttributeDataFactory $attrDataFactory, \Magento\Framework\Validator\UniversalFactory $universalFactory, RequestInterface $httpRequest, \Magento\Framework\Validator\ConfigFactory $validatorConfigFactory)
 
 setStore ($store)
 
 setEntity (\Magento\Framework\Model\AbstractModel $entity)
 
 setEntityType ($entityType)
 
 setFormCode ($formCode)
 
 getStore ()
 
 getFormCode ()
 
 getEntityType ()
 
 getEntity ()
 
 getAttribute ($attributeCode)
 
 getUserAttributes ()
 
 getSystemAttributes ()
 
 getAllowedAttributes ()
 
 prepareRequest (array $data)
 
 validateData (array $data)
 
 setIsAjaxRequest ($flag=true)
 
 getIsAjaxRequest ()
 
 ignoreInvisible ($setValue=null)
 

Protected Member Functions

 _getFormAttributeCollection ()
 
 _getFilteredFormAttributeCollection ()
 
 _getAttributeDataModel (\Magento\Eav\Model\Entity\Attribute $attribute)
 
 _isAttributeOmitted ($attribute)
 

Protected Attributes

 $_moduleName = ''
 
 $_entityTypeCode = ''
 
 $_store
 
 $_entityType
 
 $_entity
 
 $_formCode
 
 $_attributes
 
 $_systemAttributes
 
 $_userAttributes
 
 $_allowedAttributes = null
 
 $_isAjax = false
 
 $_ignoreInvisible = true
 
 $_validator = null
 
 $_storeManager
 
 $_eavConfig
 
 $_modulesReader
 
 $_attrDataFactory
 
 $_universalFactory
 
 $_httpRequest
 
 $_validatorConfigFactory
 

Detailed Description

EAV Entity Form Model

@api @SuppressWarnings(PHPMD.TooManyFields) @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 18 of file Form.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Eav\Model\Config  $eavConfig,
\Magento\Framework\Module\Dir\Reader  $modulesReader,
\Magento\Eav\Model\AttributeDataFactory  $attrDataFactory,
\Magento\Framework\Validator\UniversalFactory  $universalFactory,
RequestInterface  $httpRequest,
\Magento\Framework\Validator\ConfigFactory  $validatorConfigFactory 
)
Parameters
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Eav\Model\Config$eavConfig
\Magento\Framework\Module\Dir\Reader$modulesReader
\Magento\Eav\Model\AttributeDataFactory$attrDataFactory
\Magento\Framework\Validator\UniversalFactory$universalFactory
RequestInterface$httpRequest
\Magento\Framework\Validator\ConfigFactory$validatorConfigFactory
Exceptions

Definition at line 155 of file Form.php.

163  {
164  if (empty($this->_moduleName)) {
165  throw new \Magento\Framework\Exception\LocalizedException(__('The current module pathname is undefined.'));
166  }
167  if (empty($this->_entityTypeCode)) {
168  throw new \Magento\Framework\Exception\LocalizedException(
169  __('The current module EAV entity is undefined.')
170  );
171  }
172  $this->_storeManager = $storeManager;
173  $this->_eavConfig = $eavConfig;
174  $this->_modulesReader = $modulesReader;
175  $this->_attrDataFactory = $attrDataFactory;
176  $this->_universalFactory = $universalFactory;
177  $this->_httpRequest = $httpRequest;
178  $this->_validatorConfigFactory = $validatorConfigFactory;
179  }
$storeManager
__()
Definition: __.php:13

Member Function Documentation

◆ _getAttributeDataModel()

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

Return attribute data model by attribute

Parameters
\Magento\Eav\Model\Entity\Attribute$attribute
Returns
\Magento\Eav\Model\Attribute\Data\AbstractData

Definition at line 405 of file Form.php.

406  {
407  $dataModel = $this->_attrDataFactory->create($attribute, $this->getEntity());
408  $dataModel->setIsAjaxRequest($this->getIsAjaxRequest());
409 
410  return $dataModel;
411  }

◆ _getFilteredFormAttributeCollection()

_getFilteredFormAttributeCollection ( )
protected

Get EAV Entity Form Attribute Collection with applied filters

Returns
\Magento\Eav\Model\ResourceModel\Form\Attribute\Collection

Definition at line 198 of file Form.php.

199  {
200  return $this->_getFormAttributeCollection()->setStore(
201  $this->getStore()
202  )->setEntityType(
203  $this->getEntityType()
204  )->addFormCodeFilter(
205  $this->getFormCode()
206  )->setSortOrder();
207  }

◆ _getFormAttributeCollection()

_getFormAttributeCollection ( )
protected

Get EAV Entity Form Attribute Collection

Returns
mixed

Definition at line 186 of file Form.php.

187  {
188  return $this->_universalFactory->create(
189  str_replace('_', '\\', $this->_moduleName) . '\\Model\\ResourceModel\\Form\\Attribute\\Collection'
190  );
191  }

◆ _isAttributeOmitted()

_isAttributeOmitted (   $attribute)
protected

Whether the specified attribute needs to skip rendering/validation

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

Definition at line 631 of file Form.php.

632  {
633  if ($this->_ignoreInvisible && !$attribute->getIsVisible()) {
634  return true;
635  }
636  return false;
637  }

◆ getAllowedAttributes()

getAllowedAttributes ( )

Get not omitted attributes

Returns
array

Definition at line 390 of file Form.php.

391  {
392  if ($this->_allowedAttributes === null) {
393  // load attributes
394  $this->getAttributes();
395  }
397  }

◆ getAttribute()

getAttribute (   $attributeCode)

Return attribute instance by code or false

Parameters
string$attributeCode
Returns
\Magento\Eav\Model\Entity\Attribute|bool

Definition at line 348 of file Form.php.

349  {
350  $attributes = $this->getAttributes();
351  if (isset($attributes[$attributeCode])) {
352  return $attributes[$attributeCode];
353  }
354  return false;
355  }
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13

◆ getEntity()

getEntity ( )

Return current entity instance

Exceptions

Definition at line 308 of file Form.php.

309  {
310  if ($this->_entity === null) {
311  throw new \Magento\Framework\Exception\LocalizedException(__('The entity instance is not defined.'));
312  }
313  return $this->_entity;
314  }
__()
Definition: __.php:13

◆ getEntityType()

getEntityType ( )

Return entity type instance Return EAV entity type if entity type is not defined

Returns
\Magento\Eav\Model\Entity\Type

Definition at line 294 of file Form.php.

295  {
296  if ($this->_entityType === null) {
297  $this->setEntityType($this->_entityTypeCode);
298  }
299  return $this->_entityType;
300  }
setEntityType($entityType)
Definition: Form.php:243

◆ getFormCode()

getFormCode ( )

Return current form code

Exceptions

Definition at line 280 of file Form.php.

281  {
282  if (empty($this->_formCode)) {
283  throw new \Magento\Framework\Exception\LocalizedException(__('The form code is not defined.'));
284  }
285  return $this->_formCode;
286  }
__()
Definition: __.php:13

◆ getIsAjaxRequest()

getIsAjaxRequest ( )

Return is AJAX Request

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 586 of file Form.php.

587  {
588  return $this->_isAjax;
589  }

◆ getStore()

getStore ( )

Return current store instance

Returns
\Magento\Store\Model\Store

Definition at line 266 of file Form.php.

267  {
268  if ($this->_store === null) {
269  $this->_store = $this->_storeManager->getStore();
270  }
271  return $this->_store;
272  }

◆ getSystemAttributes()

getSystemAttributes ( )

Return array of form system attributes

Returns
array

Definition at line 376 of file Form.php.

377  {
378  if ($this->_systemAttributes === null) {
379  // load attributes
380  $this->getAttributes();
381  }
383  }

◆ getUserAttributes()

getUserAttributes ( )

Return array of form user defined attributes

Returns
array

Definition at line 362 of file Form.php.

363  {
364  if ($this->_userAttributes === null) {
365  // load attributes
366  $this->getAttributes();
367  }
368  return $this->_userAttributes;
369  }

◆ ignoreInvisible()

ignoreInvisible (   $setValue = null)

Combined getter/setter whether to omit invisible attributes during rendering/validation

Parameters
mixed$setValue
Returns
bool|$this

Definition at line 616 of file Form.php.

617  {
618  if (null !== $setValue) {
619  $this->_ignoreInvisible = (bool)$setValue;
620  return $this;
621  }
623  }

◆ prepareRequest()

prepareRequest ( array  $data)

Prepare request with data and returns it

Parameters
array$data
Returns
RequestInterface

Definition at line 419 of file Form.php.

420  {
422  $request->clearParams();
423  $request->setParams($data);
424  return $request;
425  }

◆ setEntity()

setEntity ( \Magento\Framework\Model\AbstractModel  $entity)

Set entity instance

Parameters
\Magento\Framework\Model\AbstractModel$entity
Returns
$this

Definition at line 228 of file Form.php.

229  {
230  $this->_entity = $entity;
231  if ($entity->getEntityTypeId()) {
232  $this->setEntityType($entity->getEntityTypeId());
233  }
234  return $this;
235  }
setEntityType($entityType)
Definition: Form.php:243
$entity
Definition: element.phtml:22

◆ setEntityType()

setEntityType (   $entityType)

Set entity type instance

Parameters
\Magento\Eav\Model\Entity\Type | string | int$entityType
Returns
$this

Definition at line 243 of file Form.php.

244  {
245  $this->_entityType = $this->_eavConfig->getEntityType($entityType);
246  return $this;
247  }

◆ setFormCode()

setFormCode (   $formCode)

Set form code

Parameters
string$formCode
Returns
$this

Definition at line 255 of file Form.php.

256  {
257  $this->_formCode = $formCode;
258  return $this;
259  }

◆ setIsAjaxRequest()

setIsAjaxRequest (   $flag = true)

Set is AJAX Request flag

Parameters
bool$flag
Returns
$this

Definition at line 574 of file Form.php.

575  {
576  $this->_isAjax = (bool)$flag;
577  return $this;
578  }

◆ setStore()

setStore (   $store)

Set current store

Parameters
\Magento\Store\Model\Store | string | int$store
Returns
$this @codeCoverageIgnore

Definition at line 216 of file Form.php.

217  {
218  $this->_store = $this->_storeManager->getStore($store);
219  return $this;
220  }

◆ validateData()

validateData ( array  $data)

Validate data array and return true or array of errors

Parameters
array$data
Returns
bool|array

Definition at line 481 of file Form.php.

482  {
483  $validator = $this->_getValidator($data);
484  if (!$validator->isValid($this->getEntity())) {
485  $messages = [];
486  foreach ($validator->getMessages() as $errorMessages) {
487  $messages = array_merge($messages, (array)$errorMessages);
488  }
489  return $messages;
490  }
491  return true;
492  }

Field Documentation

◆ $_allowedAttributes

$_allowedAttributes = null
protected

Definition at line 88 of file Form.php.

◆ $_attrDataFactory

$_attrDataFactory
protected

Definition at line 127 of file Form.php.

◆ $_attributes

$_attributes
protected

Definition at line 67 of file Form.php.

◆ $_eavConfig

$_eavConfig
protected

Definition at line 117 of file Form.php.

◆ $_entity

$_entity
protected

Definition at line 53 of file Form.php.

◆ $_entityType

$_entityType
protected

Definition at line 46 of file Form.php.

◆ $_entityTypeCode

$_entityTypeCode = ''
protected

Definition at line 32 of file Form.php.

◆ $_formCode

$_formCode
protected

Definition at line 60 of file Form.php.

◆ $_httpRequest

$_httpRequest
protected

Definition at line 137 of file Form.php.

◆ $_ignoreInvisible

$_ignoreInvisible = true
protected

Definition at line 102 of file Form.php.

◆ $_isAjax

$_isAjax = false
protected

Definition at line 95 of file Form.php.

◆ $_moduleName

$_moduleName = ''
protected

Definition at line 25 of file Form.php.

◆ $_modulesReader

$_modulesReader
protected

Definition at line 122 of file Form.php.

◆ $_store

$_store
protected

Definition at line 39 of file Form.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 112 of file Form.php.

◆ $_systemAttributes

$_systemAttributes
protected

Definition at line 74 of file Form.php.

◆ $_universalFactory

$_universalFactory
protected

Definition at line 132 of file Form.php.

◆ $_userAttributes

$_userAttributes
protected

Definition at line 81 of file Form.php.

◆ $_validator

$_validator = null
protected

Definition at line 107 of file Form.php.

◆ $_validatorConfigFactory

$_validatorConfigFactory
protected

Definition at line 142 of file Form.php.


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