TYPO3 CMS  TYPO3_8-7
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 
28 {
33 
37  protected $respectSubmittedDataValue = false;
38 
42  public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
43  {
44  $this->configurationManager = $configurationManager;
45  }
46 
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->viewHelperVariableContainer->get(
117  \TYPO3\CMS\Fluid\ViewHelpers\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 
246  {
247  if (!$this->isObjectAccessorMode()) {
248  return;
249  }
250 
251  if (!$this->viewHelperVariableContainer->exists(
252  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
253  'formObject'
254  )
255  ) {
256  return;
257  }
258  $propertySegments = explode('.', $this->arguments['property']);
259  // hierarchical property. If there is no "." inside (thus $propertySegments == 1), we do not need to do anything
260  if (count($propertySegments) < 2) {
261  return;
262  }
263  $formObject = $this->viewHelperVariableContainer->get(
264  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
265  'formObject'
266  );
267  $objectName = $this->viewHelperVariableContainer->get(
268  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
269  'formObjectName'
270  );
271  // If count == 2 -> we need to go through the for-loop exactly once
272  for ($i = 1; $i < count($propertySegments); $i++) {
273  $object = ObjectAccess::getPropertyPath($formObject, implode('.', array_slice($propertySegments, 0, $i)));
274  $objectName .= '[' . $propertySegments[$i - 1] . ']';
275  $hiddenIdentityField = $this->renderHiddenIdentityField($object, $objectName);
276  // Add the hidden identity field to the ViewHelperVariableContainer
277  $additionalIdentityProperties = $this->viewHelperVariableContainer->get(
278  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
279  'additionalIdentityProperties'
280  );
281  $additionalIdentityProperties[$objectName] = $hiddenIdentityField;
282  $this->viewHelperVariableContainer->addOrUpdate(
283  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
284  'additionalIdentityProperties',
285  $additionalIdentityProperties
286  );
287  }
288  }
289 
295  protected function getPropertyValue()
296  {
297  if (!$this->viewHelperVariableContainer->exists(
298  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
299  'formObject'
300  )
301  ) {
302  return null;
303  }
304  $formObject = $this->viewHelperVariableContainer->get(
305  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
306  'formObject'
307  );
308  return ObjectAccess::getPropertyPath($formObject, $this->arguments['property']);
309  }
310 
316  protected function isObjectAccessorMode()
317  {
318  return $this->hasArgument('property') && $this->viewHelperVariableContainer->exists(
319  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
320  'formObjectName'
321  );
322  }
323 
327  protected function setErrorClassAttribute()
328  {
329  if ($this->hasArgument('class')) {
330  $cssClass = $this->arguments['class'] . ' ';
331  } else {
332  $cssClass = '';
333  }
334 
335  $mappingResultsForProperty = $this->getMappingResultsForProperty();
336  if ($mappingResultsForProperty->hasErrors()) {
337  if ($this->hasArgument('errorClass')) {
338  $cssClass .= $this->arguments['errorClass'];
339  } else {
340  $cssClass .= 'error';
341  }
342  $this->tag->addAttribute('class', $cssClass);
343  }
344  }
345 
351  protected function getMappingResultsForProperty()
352  {
353  if (!$this->isObjectAccessorMode()) {
354  return new \TYPO3\CMS\Extbase\Error\Result();
355  }
356  $originalRequestMappingResults = $this->getRequest()->getOriginalRequestMappingResults();
357  $formObjectName = $this->viewHelperVariableContainer->get(
358  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
359  'formObjectName'
360  );
361  return $originalRequestMappingResults->forProperty($formObjectName)->forProperty($this->arguments['property']);
362  }
363 
370  protected function renderHiddenFieldForEmptyValue()
371  {
372  $hiddenFieldNames = [];
373  if ($this->viewHelperVariableContainer->exists(
374  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class,
375  'renderedHiddenFields'
376  )
377  ) {
378  $hiddenFieldNames = $this->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)) {
388  $hiddenFieldNames[] = $fieldName;
389  $this->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 }
static getPropertyPath($subject, $propertyPath)
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)