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
6 namespace Magento\Eav\Model;
7 
9 
18 abstract class Form
19 {
25  protected $_moduleName = '';
26 
32  protected $_entityTypeCode = '';
33 
39  protected $_store;
40 
46  protected $_entityType;
47 
53  protected $_entity;
54 
60  protected $_formCode;
61 
67  protected $_attributes;
68 
74  protected $_systemAttributes;
75 
81  protected $_userAttributes;
82 
88  protected $_allowedAttributes = null;
89 
95  protected $_isAjax = false;
96 
102  protected $_ignoreInvisible = true;
103 
107  protected $_validator = null;
108 
112  protected $_storeManager;
113 
117  protected $_eavConfig;
118 
122  protected $_modulesReader;
123 
127  protected $_attrDataFactory;
128 
133 
137  protected $_httpRequest;
138 
143 
155  public function __construct(
156  \Magento\Store\Model\StoreManagerInterface $storeManager,
157  \Magento\Eav\Model\Config $eavConfig,
158  \Magento\Framework\Module\Dir\Reader $modulesReader,
159  \Magento\Eav\Model\AttributeDataFactory $attrDataFactory,
160  \Magento\Framework\Validator\UniversalFactory $universalFactory,
161  RequestInterface $httpRequest,
162  \Magento\Framework\Validator\ConfigFactory $validatorConfigFactory
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  }
180 
186  protected function _getFormAttributeCollection()
187  {
188  return $this->_universalFactory->create(
189  str_replace('_', '\\', $this->_moduleName) . '\\Model\\ResourceModel\\Form\\Attribute\\Collection'
190  );
191  }
192 
199  {
200  return $this->_getFormAttributeCollection()->setStore(
201  $this->getStore()
202  )->setEntityType(
203  $this->getEntityType()
204  )->addFormCodeFilter(
205  $this->getFormCode()
206  )->setSortOrder();
207  }
208 
216  public function setStore($store)
217  {
218  $this->_store = $this->_storeManager->getStore($store);
219  return $this;
220  }
221 
228  public function setEntity(\Magento\Framework\Model\AbstractModel $entity)
229  {
230  $this->_entity = $entity;
231  if ($entity->getEntityTypeId()) {
232  $this->setEntityType($entity->getEntityTypeId());
233  }
234  return $this;
235  }
236 
243  public function setEntityType($entityType)
244  {
245  $this->_entityType = $this->_eavConfig->getEntityType($entityType);
246  return $this;
247  }
248 
255  public function setFormCode($formCode)
256  {
257  $this->_formCode = $formCode;
258  return $this;
259  }
260 
266  public function getStore()
267  {
268  if ($this->_store === null) {
269  $this->_store = $this->_storeManager->getStore();
270  }
271  return $this->_store;
272  }
273 
280  public function getFormCode()
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  }
287 
294  public function getEntityType()
295  {
296  if ($this->_entityType === null) {
297  $this->setEntityType($this->_entityTypeCode);
298  }
299  return $this->_entityType;
300  }
301 
308  public function getEntity()
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  }
315 
321  public function getAttributes()
322  {
323  if ($this->_attributes === null) {
324  $this->_attributes = [];
325  $this->_userAttributes = [];
327  foreach ($this->_getFilteredFormAttributeCollection() as $attribute) {
328  $this->_attributes[$attribute->getAttributeCode()] = $attribute;
329  if ($attribute->getIsUserDefined()) {
330  $this->_userAttributes[$attribute->getAttributeCode()] = $attribute;
331  } else {
332  $this->_systemAttributes[$attribute->getAttributeCode()] = $attribute;
333  }
334  if (!$this->_isAttributeOmitted($attribute)) {
335  $this->_allowedAttributes[$attribute->getAttributeCode()] = $attribute;
336  }
337  }
338  }
339  return $this->_attributes;
340  }
341 
348  public function getAttribute($attributeCode)
349  {
350  $attributes = $this->getAttributes();
351  if (isset($attributes[$attributeCode])) {
352  return $attributes[$attributeCode];
353  }
354  return false;
355  }
356 
362  public function getUserAttributes()
363  {
364  if ($this->_userAttributes === null) {
365  // load attributes
366  $this->getAttributes();
367  }
368  return $this->_userAttributes;
369  }
370 
376  public function getSystemAttributes()
377  {
378  if ($this->_systemAttributes === null) {
379  // load attributes
380  $this->getAttributes();
381  }
383  }
384 
390  public function getAllowedAttributes()
391  {
392  if ($this->_allowedAttributes === null) {
393  // load attributes
394  $this->getAttributes();
395  }
397  }
398 
406  {
407  $dataModel = $this->_attrDataFactory->create($attribute, $this->getEntity());
408  $dataModel->setIsAjaxRequest($this->getIsAjaxRequest());
409 
410  return $dataModel;
411  }
412 
419  public function prepareRequest(array $data)
420  {
422  $request->clearParams();
423  $request->setParams($data);
424  return $request;
425  }
426 
435  public function extractData(RequestInterface $request, $scope = null, $scopeOnly = true)
436  {
437  $data = [];
439  foreach ($this->getAllowedAttributes() as $attribute) {
440  $dataModel = $this->_getAttributeDataModel($attribute);
441  $dataModel->setRequestScope($scope);
442  $dataModel->setRequestScopeOnly($scopeOnly);
443  $data[$attribute->getAttributeCode()] = $dataModel->extractValue($request);
444  }
445  return $data;
446  }
447 
454  protected function _getValidator(array $data)
455  {
456  if ($this->_validator === null) {
457  $configFiles = $this->_modulesReader->getConfigurationFiles('validation.xml');
459  $validatorFactory = $this->_validatorConfigFactory->create(['configFiles' => $configFiles]);
460  $builder = $validatorFactory->createValidatorBuilder('eav_entity', 'form');
461 
462  $builder->addConfiguration(
463  'eav_data_validator',
464  ['method' => 'setAttributes', 'arguments' => [$this->getAllowedAttributes()]]
465  );
466  $builder->addConfiguration(
467  'eav_data_validator',
468  ['method' => 'setData', 'arguments' => [$data]]
469  );
470  $this->_validator = $builder->createValidator();
471  }
472  return $this->_validator;
473  }
474 
481  public function validateData(array $data)
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  }
493 
500  public function compactData(array $data)
501  {
503  foreach ($this->getAllowedAttributes() as $attribute) {
504  $dataModel = $this->_getAttributeDataModel($attribute);
505  $dataModel->setExtractedData($data);
506  if (!isset($data[$attribute->getAttributeCode()])) {
507  $data[$attribute->getAttributeCode()] = false;
508  }
509  $dataModel->compactValue($data[$attribute->getAttributeCode()]);
510  }
511 
512  return $this;
513  }
514 
521  public function restoreData(array $data)
522  {
524  foreach ($this->getAllowedAttributes() as $attribute) {
525  $dataModel = $this->_getAttributeDataModel($attribute);
526  $dataModel->setExtractedData($data);
527  if (!isset($data[$attribute->getAttributeCode()])) {
528  $data[$attribute->getAttributeCode()] = false;
529  }
530  $dataModel->restoreValue($data[$attribute->getAttributeCode()]);
531  }
532  return $this;
533  }
534 
541  public function outputData($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
542  {
543  $data = [];
545  foreach ($this->getAllowedAttributes() as $attribute) {
546  $dataModel = $this->_getAttributeDataModel($attribute);
547  $dataModel->setExtractedData($data);
548  $data[$attribute->getAttributeCode()] = $dataModel->outputValue($format);
549  }
550  return $data;
551  }
552 
558  public function resetEntityData()
559  {
561  foreach ($this->getAllowedAttributes() as $attribute) {
562  $value = $this->getEntity()->getOrigData($attribute->getAttributeCode());
563  $this->getEntity()->setData($attribute->getAttributeCode(), $value);
564  }
565  return $this;
566  }
567 
574  public function setIsAjaxRequest($flag = true)
575  {
576  $this->_isAjax = (bool)$flag;
577  return $this;
578  }
579 
586  public function getIsAjaxRequest()
587  {
588  return $this->_isAjax;
589  }
590 
596  public function initDefaultValues()
597  {
598  if (!$this->getEntity()->getId()) {
600  foreach ($this->getAttributes() as $attribute) {
601  $default = $attribute->getDefaultValue();
602  if ($default != '') {
603  $this->getEntity()->setData($attribute->getAttributeCode(), $default);
604  }
605  }
606  }
607  return $this;
608  }
609 
616  public function ignoreInvisible($setValue = null)
617  {
618  if (null !== $setValue) {
619  $this->_ignoreInvisible = (bool)$setValue;
620  return $this;
621  }
623  }
624 
631  protected function _isAttributeOmitted($attribute)
632  {
633  if ($this->_ignoreInvisible && !$attribute->getIsVisible()) {
634  return true;
635  }
636  return false;
637  }
638 }
prepareRequest(array $data)
Definition: Form.php:419
setIsAjaxRequest($flag=true)
Definition: Form.php:574
_isAttributeOmitted($attribute)
Definition: Form.php:631
validateData(array $data)
Definition: Form.php:481
setEntityType($entityType)
Definition: Form.php:243
$storeManager
__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)
Definition: Form.php:155
__()
Definition: __.php:13
ignoreInvisible($setValue=null)
Definition: Form.php:616
getAttribute($attributeCode)
Definition: Form.php:348
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$attributeCode
Definition: extend.phtml:12
_getFilteredFormAttributeCollection()
Definition: Form.php:198
setEntity(\Magento\Framework\Model\AbstractModel $entity)
Definition: Form.php:228
$entity
Definition: element.phtml:22
$attributes
Definition: matrix.phtml:13
_getAttributeDataModel(\Magento\Eav\Model\Entity\Attribute $attribute)
Definition: Form.php:405
setFormCode($formCode)
Definition: Form.php:255