TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractFormFieldViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
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 
53  public function initializeArguments()
54  {
55  parent::initializeArguments();
56  $this->registerArgument('name', 'string', 'Name of input tag');
57  $this->registerArgument('value', 'mixed', 'Value of input tag');
58  $this->registerArgument(
59  'property', '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 
81  {
82  $this->respectSubmittedDataValue = $respectSubmittedDataValue;
83  }
84 
93  protected function getName()
94  {
95  $name = $this->getNameWithoutPrefix();
96  return $this->prefixFieldName($name);
97  }
98 
104  protected function getRequest()
105  {
106  return $this->renderingContext->getControllerContext()->getRequest();
107  }
108 
114  protected function getNameWithoutPrefix()
115  {
116  if ($this->isObjectAccessorMode()) {
117  $formObjectName = $this->viewHelperVariableContainer->get(
118  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, '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(), $propertyPath
236  );
237  return $value;
238  }
239 
247  {
248  if (!$this->isObjectAccessorMode()) {
249  return;
250  }
251 
252  if (!$this->viewHelperVariableContainer->exists(
253  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, '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, 'formObject'
265  );
266  $objectName = $this->viewHelperVariableContainer->get(
267  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
268  );
269  // If count == 2 -> we need to go through the for-loop exactly once
270  for ($i = 1; $i < count($propertySegments); $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 = $this->viewHelperVariableContainer->get(
276  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties'
277  );
278  $additionalIdentityProperties[$objectName] = $hiddenIdentityField;
279  $this->viewHelperVariableContainer->addOrUpdate(
280  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties',
281  $additionalIdentityProperties
282  );
283  }
284  }
285 
291  protected function getPropertyValue()
292  {
293  if (!$this->viewHelperVariableContainer->exists(
294  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
295  )
296  ) {
297  return null;
298  }
299  $formObject = $this->viewHelperVariableContainer->get(
300  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
301  );
302  return ObjectAccess::getPropertyPath($formObject, $this->arguments['property']);
303  }
304 
310  protected function isObjectAccessorMode()
311  {
312  return $this->hasArgument('property') && $this->viewHelperVariableContainer->exists(
313  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
314  );
315  }
316 
322  protected function setErrorClassAttribute()
323  {
324  if ($this->hasArgument('class')) {
325  $cssClass = $this->arguments['class'] . ' ';
326  } else {
327  $cssClass = '';
328  }
329 
330  $mappingResultsForProperty = $this->getMappingResultsForProperty();
331  if ($mappingResultsForProperty->hasErrors()) {
332  if ($this->hasArgument('errorClass')) {
333  $cssClass .= $this->arguments['errorClass'];
334  } else {
335  $cssClass .= 'error';
336  }
337  $this->tag->addAttribute('class', $cssClass);
338  }
339  }
340 
346  protected function getMappingResultsForProperty()
347  {
348  if (!$this->isObjectAccessorMode()) {
349  return new \TYPO3\CMS\Extbase\Error\Result();
350  }
351  $originalRequestMappingResults = $this->getRequest()->getOriginalRequestMappingResults();
352  $formObjectName = $this->viewHelperVariableContainer->get(
353  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
354  );
355  return $originalRequestMappingResults->forProperty($formObjectName)->forProperty($this->arguments['property']);
356  }
357 
364  protected function renderHiddenFieldForEmptyValue()
365  {
366  $hiddenFieldNames = [];
367  if ($this->viewHelperVariableContainer->exists(
368  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
369  )
370  ) {
371  $hiddenFieldNames = $this->viewHelperVariableContainer->get(
372  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
373  );
374  }
375  $fieldName = $this->getName();
376  if (substr($fieldName, -2) === '[]') {
377  $fieldName = substr($fieldName, 0, -2);
378  }
379  if (!in_array($fieldName, $hiddenFieldNames)) {
380  $hiddenFieldNames[] = $fieldName;
381  $this->viewHelperVariableContainer->addOrUpdate(
382  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields', $hiddenFieldNames
383  );
384  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="" />';
385  }
386  return '';
387  }
388 }
static getPropertyPath($subject, $propertyPath)
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)