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 
10 
15 class Form
16 {
20  const IGNORE_INVISIBLE = true;
21 
22  const DONT_IGNORE_INVISIBLE = false;
23 
28 
33 
37  protected $_elementFactory;
38 
42  protected $_entityType;
43 
47  protected $_formCode;
48 
52  protected $_ignoreInvisible = true;
53 
57  protected $_filterAttributes = [];
58 
62  protected $_isAjax = false;
63 
69  protected $_attributeValues = [];
70 
74  protected $_httpRequest;
75 
79  protected $_modulesReader;
80 
85 
89  protected $_validator;
90 
94  protected $_attributes;
95 
112  public function __construct(
113  CustomerMetadataInterface $customerMetadataService,
114  AddressMetadataInterface $addressMetadataService,
115  ElementFactory $elementFactory,
116  \Magento\Framework\App\RequestInterface $httpRequest,
117  \Magento\Framework\Module\Dir\Reader $modulesReader,
118  \Magento\Framework\Validator\ConfigFactory $validatorConfigFactory,
119  $entityType,
120  $formCode,
121  array $attributeValues = [],
122  $ignoreInvisible = self::IGNORE_INVISIBLE,
123  $filterAttributes = [],
124  $isAjax = false
125  ) {
126  $this->_customerMetadataService = $customerMetadataService;
127  $this->_addressMetadataService = $addressMetadataService;
128  $this->_elementFactory = $elementFactory;
129  $this->_attributeValues = $attributeValues;
130  $this->_entityType = $entityType;
131  $this->_formCode = $formCode;
132  $this->_ignoreInvisible = $ignoreInvisible;
133  $this->_filterAttributes = $filterAttributes;
134  $this->_isAjax = $isAjax;
135  $this->_httpRequest = $httpRequest;
136  $this->_modulesReader = $modulesReader;
137  $this->_validatorConfigFactory = $validatorConfigFactory;
138  }
139 
146  public function getAttributes()
147  {
148  if (!isset($this->_attributes)) {
149  if ($this->_entityType === \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
150  $this->_attributes = $this->_customerMetadataService->getAttributes($this->_formCode);
151  } elseif ($this->_entityType === \Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
152  $this->_attributes = $this->_addressMetadataService->getAttributes($this->_formCode);
153  } else {
154  throw new \LogicException('Undefined entity type: ' . $this->_entityType);
155  }
156  }
157  return $this->_attributes;
158  }
159 
166  public function getAttribute($attributeCode)
167  {
168  $attributes = $this->getAttributes();
169  if (isset($attributes[$attributeCode])) {
170  return $attributes[$attributeCode];
171  }
172  return false;
173  }
174 
180  public function getUserAttributes()
181  {
182  $result = [];
183  foreach ($this->getAttributes() as $attribute) {
184  if ($attribute->isUserDefined()) {
185  $result[$attribute->getAttributeCode()] = $attribute;
186  }
187  }
188  return $result;
189  }
190 
196  public function getSystemAttributes()
197  {
198  $result = [];
199  foreach ($this->getAttributes() as $attribute) {
200  if (!$attribute->isUserDefined()) {
201  $result[$attribute->getAttributeCode()] = $attribute;
202  }
203  }
204  return $result;
205  }
206 
212  public function getAllowedAttributes()
213  {
214  $attributes = $this->getAttributes();
215  foreach ($attributes as $attributeCode => $attribute) {
216  if ($this->_ignoreInvisible && !$attribute->isVisible() || in_array(
217  $attribute->getAttributeCode(),
219  )
220  ) {
221  unset($attributes[$attributeCode]);
222  }
223  }
224  return $attributes;
225  }
226 
235  public function extractData(\Magento\Framework\App\RequestInterface $request, $scope = null, $scopeOnly = true)
236  {
237  $data = [];
238  foreach ($this->getAllowedAttributes() as $attribute) {
239  $dataModel = $this->_getAttributeDataModel($attribute);
240  $dataModel->setRequestScope($scope);
241  $dataModel->setRequestScopeOnly($scopeOnly);
242  $data[$attribute->getAttributeCode()] = $dataModel->extractValue($request);
243  }
244  return $data;
245  }
246 
253  public function compactData(array $data)
254  {
255  foreach ($this->getAllowedAttributes() as $attribute) {
256  $dataModel = $this->_getAttributeDataModel($attribute);
257  $dataModel->setExtractedData($data);
258  if (!isset($data[$attribute->getAttributeCode()])) {
259  $data[$attribute->getAttributeCode()] = false;
260  }
261  $attributeCode = $attribute->getAttributeCode();
262  $this->_attributeValues[$attributeCode] = $dataModel->compactValue($data[$attributeCode]);
263  }
264 
266  }
267 
274  public function restoreData(array $data)
275  {
276  foreach ($this->getAllowedAttributes() as $attribute) {
277  $dataModel = $this->_getAttributeDataModel($attribute);
278  $dataModel->setExtractedData($data);
279  if (!isset($data[$attribute->getAttributeCode()])) {
280  $data[$attribute->getAttributeCode()] = false;
281  }
282  $attributeCode = $attribute->getAttributeCode();
283  $this->_attributeValues[$attributeCode] = $dataModel->restoreValue($data[$attributeCode]);
284  }
286  }
287 
294  protected function _getAttributeDataModel($attribute)
295  {
296  $dataModel = $this->_elementFactory->create(
297  $attribute,
298  isset(
299  $this->_attributeValues[$attribute->getAttributeCode()]
300  ) ? $this->_attributeValues[$attribute->getAttributeCode()] : null,
303  );
304  return $dataModel;
305  }
306 
313  public function prepareRequest(array $data)
314  {
316  $request->clearParams();
317  $request->setParams($data);
318  return $request;
319  }
320 
327  protected function _getValidator(array $data)
328  {
329  if ($this->_validator !== null) {
330  return $this->_validator;
331  }
332 
333  $configFiles = $this->_modulesReader->getConfigurationFiles('validation.xml');
334  $validatorFactory = $this->_validatorConfigFactory->create(['configFiles' => $configFiles]);
335  $builder = $validatorFactory->createValidatorBuilder('customer', 'form');
336 
337  $builder->addConfiguration(
338  'metadata_data_validator',
339  ['method' => 'setAttributes', 'arguments' => [$this->getAllowedAttributes()]]
340  );
341  $builder->addConfiguration(
342  'metadata_data_validator',
343  ['method' => 'setData', 'arguments' => [$data]]
344  );
345  $builder->addConfiguration(
346  'metadata_data_validator',
347  ['method' => 'setEntityType', 'arguments' => [$this->_entityType]]
348  );
349  $this->_validator = $builder->createValidator();
350 
351  return $this->_validator;
352  }
353 
360  public function validateData(array $data)
361  {
362  $validator = $this->_getValidator($data);
363  if (!$validator->isValid(false)) {
364  $messages = [];
365  foreach ($validator->getMessages() as $errorMessages) {
366  $messages = array_merge($messages, (array)$errorMessages);
367  }
368  return $messages;
369  }
370  return true;
371  }
372 
379  public function outputData($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
380  {
381  $result = [];
382  foreach ($this->getAllowedAttributes() as $attribute) {
383  $dataModel = $this->_getAttributeDataModel($attribute);
384  $result[$attribute->getAttributeCode()] = $dataModel->outputValue($format);
385  }
386  return $result;
387  }
388 
395  public function setInvisibleIgnored($ignoreInvisible)
396  {
397  $this->_ignoreInvisible = $ignoreInvisible;
398  }
399 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
outputData($format=\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
Definition: Form.php:379
$format
Definition: list.phtml:12
$attributeCode
Definition: extend.phtml:12
setInvisibleIgnored($ignoreInvisible)
Definition: Form.php:395
$attributes
Definition: matrix.phtml:13
extractData(\Magento\Framework\App\RequestInterface $request, $scope=null, $scopeOnly=true)
Definition: Form.php:235
__construct(CustomerMetadataInterface $customerMetadataService, AddressMetadataInterface $addressMetadataService, ElementFactory $elementFactory, \Magento\Framework\App\RequestInterface $httpRequest, \Magento\Framework\Module\Dir\Reader $modulesReader, \Magento\Framework\Validator\ConfigFactory $validatorConfigFactory, $entityType, $formCode, array $attributeValues=[], $ignoreInvisible=self::IGNORE_INVISIBLE, $filterAttributes=[], $isAjax=false)
Definition: Form.php:112