‪TYPO3CMS  9.5
AbstractFormFieldViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
26 {
30  protected ‪$configurationManager;
31 
35  protected ‪$respectSubmittedDataValue = false;
36 
41  {
42  $this->configurationManager = ‪$configurationManager;
43  }
44 
48  public function ‪initializeArguments()
49  {
50  parent::initializeArguments();
51  $this->registerArgument('name', 'string', 'Name of input tag');
52  $this->registerArgument('value', 'mixed', 'Value of input tag');
53  $this->registerArgument(
54  'property',
55  'string',
56  'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.'
57  );
58  }
59 
65  public function ‪getRespectSubmittedDataValue()
66  {
68  }
69 
76  {
77  $this->respectSubmittedDataValue = ‪$respectSubmittedDataValue;
78  }
79 
88  protected function ‪getName()
89  {
90  $name = $this->‪getNameWithoutPrefix();
91  return $this->‪prefixFieldName($name);
92  }
93 
99  protected function ‪getRequest()
100  {
101  return $this->renderingContext->getControllerContext()->getRequest();
102  }
103 
109  protected function ‪getNameWithoutPrefix()
110  {
111  if ($this->‪isObjectAccessorMode()) {
112  $formObjectName = $this->renderingContext->getViewHelperVariableContainer()->get(
113  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
114  'formObjectName'
115  );
116  if (!empty($formObjectName)) {
117  $propertySegments = explode('.', $this->arguments['property'] ?? '');
118  $propertyPath = '';
119  foreach ($propertySegments as $segment) {
120  $propertyPath .= '[' . $segment . ']';
121  }
122  $name = $formObjectName . $propertyPath;
123  } else {
124  $name = $this->arguments['property'] ?? '';
125  }
126  } else {
127  $name = $this->arguments['name'] ?? '';
128  }
129  if ($this->hasArgument('value') && is_object($this->arguments['value'])) {
130  // @todo Use $this->persistenceManager->isNewObject() once it is implemented
131  if (null !== $this->persistenceManager->getIdentifierByObject($this->arguments['value'])) {
132  $name .= '[__identity]';
133  }
134  }
135  return $name;
136  }
137 
149  protected function ‪getValueAttribute()
150  {
151  $value = null;
152 
153  if ($this->respectSubmittedDataValue) {
154  $value = $this->‪getValueFromSubmittedFormData($value);
155  } elseif ($this->hasArgument('value')) {
156  $value = $this->arguments['value'];
157  } elseif ($this->‪isObjectAccessorMode()) {
158  $value = $this->‪getPropertyValue();
159  }
160 
161  $value = $this->‪convertToPlainValue($value);
162  return $value;
163  }
164 
177  protected function ‪getValueFromSubmittedFormData($value)
178  {
179  $submittedFormData = null;
180  if ($this->‪hasMappingErrorOccurred()) {
181  $submittedFormData = $this->‪getLastSubmittedFormData();
182  }
183  if ($submittedFormData !== null) {
184  $value = $submittedFormData;
185  } elseif ($this->hasArgument('value')) {
186  $value = $this->arguments['value'];
187  } elseif ($this->‪isObjectAccessorMode()) {
188  $value = $this->‪getPropertyValue();
189  }
190 
191  return $value;
192  }
193 
200  protected function ‪convertToPlainValue($value)
201  {
202  if (is_object($value)) {
203  $identifier = $this->persistenceManager->getIdentifierByObject($value);
204  if ($identifier !== null) {
205  $value = $identifier;
206  }
207  }
208  return $value;
209  }
210 
216  protected function ‪hasMappingErrorOccurred()
217  {
218  return $this->renderingContext->getControllerContext()->getRequest()->getOriginalRequest() !== null;
219  }
220 
227  protected function ‪getLastSubmittedFormData()
228  {
229  $propertyPath = rtrim(preg_replace('/(\\]\\[|\\[|\\])/', '.', $this->‪getNameWithoutPrefix()), '.');
231  $this->renderingContext->getControllerContext()->getRequest()->getOriginalRequest()->getArguments(),
232  $propertyPath
233  );
234  return $value;
235  }
236 
241  protected function ‪addAdditionalIdentityPropertiesIfNeeded()
242  {
243  if (!$this->‪isObjectAccessorMode()) {
244  return;
245  }
246 
247  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
248  if (!$viewHelperVariableContainer->exists(
249  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
250  'formObject'
251  )
252  ) {
253  return;
254  }
255  $propertySegments = explode('.', $this->arguments['property']);
256  // hierarchical property. If there is no "." inside (thus $propertySegments == 1), we do not need to do anything
257  if (count($propertySegments) < 2) {
258  return;
259  }
260  $formObject = $viewHelperVariableContainer->get(
261  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
262  'formObject'
263  );
264  $objectName = $viewHelperVariableContainer->get(
265  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
266  'formObjectName'
267  );
268  // If count == 2 -> we need to go through the for-loop exactly once
269  $propertySegmentsCount = count($propertySegments);
270  for ($i = 1; $i < $propertySegmentsCount; $i++) {
271  $object = ‪ObjectAccess::getPropertyPath($formObject, implode('.', array_slice($propertySegments, 0, $i)));
272  $objectName .= '[' . $propertySegments[$i - 1] . ']';
273  $hiddenIdentityField = $this->‪renderHiddenIdentityField($object, $objectName);
274  // Add the hidden identity field to the ViewHelperVariableContainer
275  $additionalIdentityProperties = $viewHelperVariableContainer->get(
276  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
277  'additionalIdentityProperties'
278  );
279  $additionalIdentityProperties[$objectName] = $hiddenIdentityField;
280  $viewHelperVariableContainer->addOrUpdate(
281  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
282  'additionalIdentityProperties',
283  $additionalIdentityProperties
284  );
285  }
286  }
287 
293  protected function ‪getPropertyValue()
294  {
295  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
296  if (!$viewHelperVariableContainer->exists(
297  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
298  'formObject'
299  )
300  ) {
301  return null;
302  }
303  $formObject = $viewHelperVariableContainer->get(
304  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
305  'formObject'
306  );
307  return ‪ObjectAccess::getPropertyPath($formObject, $this->arguments['property']);
308  }
309 
315  protected function ‪isObjectAccessorMode()
316  {
317  return $this->hasArgument('property') && $this->renderingContext->getViewHelperVariableContainer()->exists(
318  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
319  'formObjectName'
320  );
321  }
322 
326  protected function ‪setErrorClassAttribute()
327  {
328  if ($this->hasArgument('class')) {
329  $cssClass = $this->arguments['class'] . ' ';
330  } else {
331  $cssClass = '';
332  }
333 
334  $mappingResultsForProperty = $this->‪getMappingResultsForProperty();
335  if ($mappingResultsForProperty->hasErrors()) {
336  if ($this->hasArgument('errorClass')) {
337  $cssClass .= $this->arguments['errorClass'];
338  } else {
339  $cssClass .= 'error';
340  }
341  $this->tag->addAttribute('class', $cssClass);
342  }
343  }
344 
350  protected function ‪getMappingResultsForProperty()
351  {
352  if (!$this->‪isObjectAccessorMode()) {
353  return new \TYPO3\CMS\Extbase\Error\Result();
354  }
355  $originalRequestMappingResults = $this->‪getRequest()->getOriginalRequestMappingResults();
356  $formObjectName = $this->renderingContext->getViewHelperVariableContainer()->get(
357  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
358  'formObjectName'
359  );
360  return $originalRequestMappingResults->forProperty($formObjectName)->forProperty($this->arguments['property']);
361  }
362 
369  protected function ‪renderHiddenFieldForEmptyValue()
370  {
371  $hiddenFieldNames = [];
372  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
373  if ($viewHelperVariableContainer->exists(
374  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
375  'renderedHiddenFields'
376  )
377  ) {
378  $hiddenFieldNames = $viewHelperVariableContainer->get(
379  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
380  'renderedHiddenFields'
381  );
382  }
383  $fieldName = $this->‪getName();
384  if (substr($fieldName, -2) === '[]') {
385  $fieldName = substr($fieldName, 0, -2);
386  }
387  if (!in_array($fieldName, $hiddenFieldNames, true)) {
388  $hiddenFieldNames[] = $fieldName;
389  $viewHelperVariableContainer->addOrUpdate(
390  \‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
391  'renderedHiddenFields',
392  $hiddenFieldNames
393  );
394  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="" />';
395  }
396  return '';
397  }
398 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:324
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\$respectSubmittedDataValue
‪bool $respectSubmittedDataValue
Definition: AbstractFormFieldViewHelper.php:33
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\convertToPlainValue
‪mixed convertToPlainValue($value)
Definition: AbstractFormFieldViewHelper.php:198
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setRespectSubmittedDataValue
‪setRespectSubmittedDataValue($respectSubmittedDataValue)
Definition: AbstractFormFieldViewHelper.php:73
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getRespectSubmittedDataValue
‪bool getRespectSubmittedDataValue()
Definition: AbstractFormFieldViewHelper.php:63
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\injectConfigurationManager
‪injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
Definition: AbstractFormFieldViewHelper.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getPropertyValue
‪mixed getPropertyValue()
Definition: AbstractFormFieldViewHelper.php:291
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractFormFieldViewHelper.php:46
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getLastSubmittedFormData
‪mixed getLastSubmittedFormData()
Definition: AbstractFormFieldViewHelper.php:225
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\renderHiddenIdentityField
‪string renderHiddenIdentityField($object, $name)
Definition: AbstractFormViewHelper.php:74
‪TYPO3
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:22
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:29
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue()
Definition: AbstractFormFieldViewHelper.php:367
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\$configurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManagerInterface $configurationManager
Definition: AbstractFormFieldViewHelper.php:29
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\isObjectAccessorMode
‪bool isObjectAccessorMode()
Definition: AbstractFormFieldViewHelper.php:313
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getRequest
‪TYPO3 CMS Extbase Mvc Request getRequest()
Definition: AbstractFormFieldViewHelper.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getMappingResultsForProperty
‪TYPO3 CMS Extbase Error Result getMappingResultsForProperty()
Definition: AbstractFormFieldViewHelper.php:348
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getNameWithoutPrefix
‪string getNameWithoutPrefix()
Definition: AbstractFormFieldViewHelper.php:107
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getPropertyPath
‪static mixed getPropertyPath($subject, $propertyPath)
Definition: ObjectAccess.php:144
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper
Definition: AbstractFormViewHelper.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueFromSubmittedFormData
‪mixed getValueFromSubmittedFormData($value)
Definition: AbstractFormFieldViewHelper.php:175
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\hasMappingErrorOccurred
‪bool hasMappingErrorOccurred()
Definition: AbstractFormFieldViewHelper.php:214
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\addAdditionalIdentityPropertiesIfNeeded
‪addAdditionalIdentityPropertiesIfNeeded()
Definition: AbstractFormFieldViewHelper.php:239
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\prefixFieldName
‪string prefixFieldName($fieldName)
Definition: AbstractFormViewHelper.php:45
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:86
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:147