‪TYPO3CMS  10.4
AbstractFormFieldViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
30 {
34  protected ‪$configurationManager;
35 
39  protected ‪$respectSubmittedDataValue = false;
40 
45  {
46  $this->configurationManager = ‪$configurationManager;
47  }
48 
52  public function ‪initializeArguments()
53  {
54  parent::initializeArguments();
55  $this->registerArgument('name', 'string', 'Name of input tag');
56  $this->registerArgument('value', 'mixed', 'Value of input tag');
57  $this->registerArgument(
58  'property',
59  'string',
60  'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.'
61  );
62  }
63 
69  public function ‪getRespectSubmittedDataValue()
70  {
72  }
73 
80  {
81  $this->respectSubmittedDataValue = ‪$respectSubmittedDataValue;
82  }
83 
92  protected function ‪getName()
93  {
94  $name = $this->‪getNameWithoutPrefix();
95  return $this->‪prefixFieldName($name);
96  }
97 
103  protected function ‪getRequest()
104  {
105  return $this->renderingContext->getControllerContext()->getRequest();
106  }
107 
113  protected function ‪getNameWithoutPrefix()
114  {
115  if ($this->‪isObjectAccessorMode()) {
116  $formObjectName = $this->renderingContext->getViewHelperVariableContainer()->get(
117  FormViewHelper::class,
118  'formObjectName'
119  );
120  if (!empty($formObjectName)) {
121  $propertySegments = explode('.', $this->arguments['property'] ?? '');
122  $propertyPath = '';
123  foreach ($propertySegments as $segment) {
124  $propertyPath .= '[' . $segment . ']';
125  }
126  $name = $formObjectName . $propertyPath;
127  } else {
128  $name = $this->arguments['property'] ?? '';
129  }
130  } else {
131  $name = $this->arguments['name'] ?? '';
132  }
133  if ($this->hasArgument('value') && is_object($this->arguments['value'])) {
134  // @todo Use $this->persistenceManager->isNewObject() once it is implemented
135  if (null !== $this->persistenceManager->getIdentifierByObject($this->arguments['value'])) {
136  $name .= '[__identity]';
137  }
138  }
139  return $name;
140  }
141 
153  protected function ‪getValueAttribute()
154  {
155  $value = null;
156 
157  if ($this->respectSubmittedDataValue) {
158  $value = $this->‪getValueFromSubmittedFormData($value);
159  } elseif ($this->hasArgument('value')) {
160  $value = $this->arguments['value'];
161  } elseif ($this->‪isObjectAccessorMode()) {
162  $value = $this->‪getPropertyValue();
163  }
164 
165  $value = $this->‪convertToPlainValue($value);
166  return $value;
167  }
168 
181  protected function ‪getValueFromSubmittedFormData($value)
182  {
183  $submittedFormData = null;
184  if ($this->‪hasMappingErrorOccurred()) {
185  $submittedFormData = $this->‪getLastSubmittedFormData();
186  }
187  if ($submittedFormData !== null) {
188  $value = $submittedFormData;
189  } elseif ($this->hasArgument('value')) {
190  $value = $this->arguments['value'];
191  } elseif ($this->‪isObjectAccessorMode()) {
192  $value = $this->‪getPropertyValue();
193  }
194 
195  return $value;
196  }
197 
204  protected function ‪convertToPlainValue($value)
205  {
206  if (is_object($value)) {
207  $identifier = $this->persistenceManager->getIdentifierByObject($value);
208  if ($identifier !== null) {
209  $value = $identifier;
210  }
211  }
212  return $value;
213  }
214 
220  protected function ‪hasMappingErrorOccurred()
221  {
222  return $this->renderingContext->getControllerContext()->getRequest()->getOriginalRequest() !== null;
223  }
224 
231  protected function ‪getLastSubmittedFormData()
232  {
233  $propertyPath = rtrim(preg_replace('/(\\]\\[|\\[|\\])/', '.', $this->‪getNameWithoutPrefix()) ?? '', '.');
235  $this->renderingContext->getControllerContext()->getRequest()->getOriginalRequest()->getArguments(),
236  $propertyPath
237  );
238  return $value;
239  }
240 
245  protected function ‪addAdditionalIdentityPropertiesIfNeeded()
246  {
247  if (!$this->‪isObjectAccessorMode()) {
248  return;
249  }
250 
251  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
252  if (!$viewHelperVariableContainer->exists(
253  FormViewHelper::class,
254  'formObject'
255  )
256  ) {
257  return;
258  }
259  $propertySegments = explode('.', $this->arguments['property']);
260  // hierarchical property. If there is no "." inside (thus $propertySegments == 1), we do not need to do anything
261  if (count($propertySegments) < 2) {
262  return;
263  }
264  $formObject = $viewHelperVariableContainer->get(
265  FormViewHelper::class,
266  'formObject'
267  );
268  $objectName = $viewHelperVariableContainer->get(
269  FormViewHelper::class,
270  'formObjectName'
271  );
272  // If count == 2 -> we need to go through the for-loop exactly once
273  $propertySegmentsCount = count($propertySegments);
274  for ($i = 1; $i < $propertySegmentsCount; $i++) {
275  $object = ‪ObjectAccess::getPropertyPath($formObject, implode('.', array_slice($propertySegments, 0, $i)));
276  $objectName .= '[' . $propertySegments[$i - 1] . ']';
277  $hiddenIdentityField = $this->‪renderHiddenIdentityField($object, $objectName);
278  // Add the hidden identity field to the ViewHelperVariableContainer
279  $additionalIdentityProperties = $viewHelperVariableContainer->get(
280  FormViewHelper::class,
281  'additionalIdentityProperties'
282  );
283  $additionalIdentityProperties[$objectName] = $hiddenIdentityField;
284  $viewHelperVariableContainer->addOrUpdate(
285  FormViewHelper::class,
286  'additionalIdentityProperties',
287  $additionalIdentityProperties
288  );
289  }
290  }
291 
297  protected function ‪getPropertyValue()
298  {
299  if ($this->arguments['property'] === null) {
300  return null;
301  }
302  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
303  if (!$viewHelperVariableContainer->exists(
304  FormViewHelper::class,
305  'formObject'
306  )
307  ) {
308  return null;
309  }
310  $formObject = $viewHelperVariableContainer->get(
311  FormViewHelper::class,
312  'formObject'
313  );
314  return ‪ObjectAccess::getPropertyPath($formObject, $this->arguments['property']);
315  }
316 
322  protected function ‪isObjectAccessorMode()
323  {
324  return $this->hasArgument('property') && $this->renderingContext->getViewHelperVariableContainer()->exists(
325  FormViewHelper::class,
326  'formObjectName'
327  );
328  }
329 
333  protected function ‪setErrorClassAttribute()
334  {
335  if ($this->hasArgument('class')) {
336  $cssClass = $this->arguments['class'] . ' ';
337  } else {
338  $cssClass = '';
339  }
340 
341  $mappingResultsForProperty = $this->‪getMappingResultsForProperty();
342  if ($mappingResultsForProperty->hasErrors()) {
343  if ($this->hasArgument('errorClass')) {
344  $cssClass .= $this->arguments['errorClass'];
345  } else {
346  $cssClass .= 'error';
347  }
348  $this->tag->addAttribute('class', $cssClass);
349  }
350  }
351 
357  protected function ‪getMappingResultsForProperty()
358  {
359  if (!$this->‪isObjectAccessorMode()) {
360  return new ‪Result();
361  }
362  $originalRequestMappingResults = $this->‪getRequest()->getOriginalRequestMappingResults();
363  $formObjectName = $this->renderingContext->getViewHelperVariableContainer()->get(
364  FormViewHelper::class,
365  'formObjectName'
366  );
367  return $originalRequestMappingResults->forProperty($formObjectName)->forProperty($this->arguments['property']);
368  }
369 
376  protected function ‪renderHiddenFieldForEmptyValue()
377  {
378  $hiddenFieldNames = [];
379  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
380  if ($viewHelperVariableContainer->exists(
381  FormViewHelper::class,
382  'renderedHiddenFields'
383  )
384  ) {
385  $hiddenFieldNames = $viewHelperVariableContainer->get(
386  FormViewHelper::class,
387  'renderedHiddenFields'
388  );
389  }
390  $fieldName = $this->‪getName();
391  if (substr($fieldName, -2) === '[]') {
392  $fieldName = substr($fieldName, 0, -2);
393  }
394  if (!in_array($fieldName, $hiddenFieldNames, true)) {
395  $hiddenFieldNames[] = $fieldName;
396  $viewHelperVariableContainer->addOrUpdate(
397  FormViewHelper::class,
398  'renderedHiddenFields',
399  $hiddenFieldNames
400  );
401  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="" />';
402  }
403  return '';
404  }
405 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:331
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\$respectSubmittedDataValue
‪bool $respectSubmittedDataValue
Definition: AbstractFormFieldViewHelper.php:37
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\convertToPlainValue
‪mixed convertToPlainValue($value)
Definition: AbstractFormFieldViewHelper.php:202
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setRespectSubmittedDataValue
‪setRespectSubmittedDataValue($respectSubmittedDataValue)
Definition: AbstractFormFieldViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getRespectSubmittedDataValue
‪bool getRespectSubmittedDataValue()
Definition: AbstractFormFieldViewHelper.php:67
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getPropertyValue
‪mixed getPropertyValue()
Definition: AbstractFormFieldViewHelper.php:295
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractFormFieldViewHelper.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:16
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getLastSubmittedFormData
‪mixed getLastSubmittedFormData()
Definition: AbstractFormFieldViewHelper.php:229
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\renderHiddenIdentityField
‪string renderHiddenIdentityField($object, $name)
Definition: AbstractFormViewHelper.php:79
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\injectConfigurationManager
‪injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
Definition: AbstractFormFieldViewHelper.php:42
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper
Definition: FormViewHelper.php:61
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue()
Definition: AbstractFormFieldViewHelper.php:374
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\$configurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManagerInterface $configurationManager
Definition: AbstractFormFieldViewHelper.php:33
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\isObjectAccessorMode
‪bool isObjectAccessorMode()
Definition: AbstractFormFieldViewHelper.php:320
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getRequest
‪TYPO3 CMS Extbase Mvc Request getRequest()
Definition: AbstractFormFieldViewHelper.php:101
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getMappingResultsForProperty
‪TYPO3 CMS Extbase Error Result getMappingResultsForProperty()
Definition: AbstractFormFieldViewHelper.php:355
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getNameWithoutPrefix
‪string getNameWithoutPrefix()
Definition: AbstractFormFieldViewHelper.php:111
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper
Definition: AbstractFormViewHelper.php:31
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueFromSubmittedFormData
‪mixed getValueFromSubmittedFormData($value)
Definition: AbstractFormFieldViewHelper.php:179
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getPropertyPath
‪static mixed getPropertyPath($subject, string $propertyPath)
Definition: ObjectAccess.php:139
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\hasMappingErrorOccurred
‪bool hasMappingErrorOccurred()
Definition: AbstractFormFieldViewHelper.php:218
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\addAdditionalIdentityPropertiesIfNeeded
‪addAdditionalIdentityPropertiesIfNeeded()
Definition: AbstractFormFieldViewHelper.php:243
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\prefixFieldName
‪string prefixFieldName($fieldName)
Definition: AbstractFormViewHelper.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:90
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:151