TYPO3 CMS  TYPO3_7-6
AbstractFormFieldViewHelper.php
Go to the documentation of this file.
1 <?php
2 
4 
5 /* *
6  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
7  * *
8  * It is free software; you can redistribute it and/or modify it under *
9  * the terms of the GNU Lesser General Public License, either version 3 *
10  * of the License, or (at your option) any later version. *
11  * *
12  * *
13  * This script is distributed in the hope that it will be useful, but *
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
15  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with the script. *
20  * If not, see http://www.gnu.org/licenses/lgpl.html *
21  * *
22  * The TYPO3 project - inspiring people to share! *
23  * */
24 
27 
37 {
42 
46  protected $respectSubmittedDataValue = false;
47 
51  public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
52  {
53  $this->configurationManager = $configurationManager;
54  }
55 
62  public function initializeArguments()
63  {
64  parent::initializeArguments();
65  $this->registerArgument('name', 'string', 'Name of input tag');
66  $this->registerArgument('value', 'mixed', 'Value of input tag');
67  $this->registerArgument(
68  'property', 'string',
69  'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.'
70  );
71  }
72 
78  public function getRespectSubmittedDataValue()
79  {
81  }
82 
90  {
91  $this->respectSubmittedDataValue = $respectSubmittedDataValue;
92  }
93 
102  protected function getName()
103  {
104  $name = $this->getNameWithoutPrefix();
105  return $this->prefixFieldName($name);
106  }
107 
113  protected function getRequest()
114  {
115  return $this->controllerContext->getRequest();
116  }
117 
123  protected function getNameWithoutPrefix()
124  {
125  if ($this->isObjectAccessorMode()) {
126  $formObjectName = $this->viewHelperVariableContainer->get(
127  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
128  );
129  if (!empty($formObjectName)) {
130  $propertySegments = explode('.', $this->arguments['property']);
131  $propertyPath = '';
132  foreach ($propertySegments as $segment) {
133  $propertyPath .= '[' . $segment . ']';
134  }
135  $name = $formObjectName . $propertyPath;
136  } else {
137  $name = $this->arguments['property'];
138  }
139  } else {
140  $name = $this->arguments['name'];
141  }
142  if ($this->hasArgument('value') && is_object($this->arguments['value'])) {
143  // @todo Use $this->persistenceManager->isNewObject() once it is implemented
144  if (null !== $this->persistenceManager->getIdentifierByObject($this->arguments['value'])) {
145  $name .= '[__identity]';
146  }
147  }
148  return $name;
149  }
150 
159  protected function getValue($convertObjects = true)
160  {
161  $value = null;
163 
164  if ($this->hasArgument('value')) {
165  $value = $this->arguments['value'];
166  } elseif ($this->isObjectAccessorMode()) {
167  if ($this->hasMappingErrorOccurred()) {
168  $value = $this->getLastSubmittedFormData();
169  } else {
170  $value = $this->getPropertyValue();
171  }
173  }
174 
175  if ($convertObjects) {
176  $value = $this->convertToPlainValue($value);
177  }
178  return $value;
179  }
180 
192  protected function getValueAttribute()
193  {
194  $value = null;
195 
196  if ($this->respectSubmittedDataValue) {
197  $value = $this->getValueFromSubmittedFormData($value);
198  } elseif ($this->hasArgument('value')) {
199  $value = $this->arguments['value'];
200  } elseif ($this->isObjectAccessorMode()) {
201  $value = $this->getPropertyValue();
202  }
203 
204  $value = $this->convertToPlainValue($value);
205  return $value;
206  }
207 
220  protected function getValueFromSubmittedFormData($value)
221  {
222  $submittedFormData = null;
223  if ($this->hasMappingErrorOccurred()) {
224  $submittedFormData = $this->getLastSubmittedFormData();
225  }
226  if ($submittedFormData !== null) {
227  $value = $submittedFormData;
228  } elseif ($this->hasArgument('value')) {
229  $value = $this->arguments['value'];
230  } elseif ($this->isObjectAccessorMode()) {
231  $value = $this->getPropertyValue();
232  }
233 
234  return $value;
235  }
236 
243  protected function convertToPlainValue($value)
244  {
245  if (is_object($value)) {
246  $identifier = $this->persistenceManager->getIdentifierByObject($value);
247  if ($identifier !== null) {
248  $value = $identifier;
249  }
250  }
251  return $value;
252  }
253 
259  protected function hasMappingErrorOccurred()
260  {
261  return $this->getRequest()->getOriginalRequest() !== null;
262  }
263 
270  protected function getLastSubmittedFormData()
271  {
272  $propertyPath = rtrim(preg_replace('/(\\]\\[|\\[|\\])/', '.', $this->getNameWithoutPrefix()), '.');
274  $this->controllerContext->getRequest()->getOriginalRequest()->getArguments(), $propertyPath
275  );
276  return $value;
277  }
278 
286  {
287  if (!$this->isObjectAccessorMode()) {
288  return;
289  }
290 
291  if (!$this->viewHelperVariableContainer->exists(
292  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
293  )
294  ) {
295  return;
296  }
297  $propertySegments = explode('.', $this->arguments['property']);
298  // hierarchical property. If there is no "." inside (thus $propertySegments == 1), we do not need to do anything
299  if (count($propertySegments) < 2) {
300  return;
301  }
302  $formObject = $this->viewHelperVariableContainer->get(
303  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
304  );
305  $objectName = $this->viewHelperVariableContainer->get(
306  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
307  );
308  // If count == 2 -> we need to go through the for-loop exactly once
309  for ($i = 1; $i < count($propertySegments); $i++) {
310  $object = ObjectAccess::getPropertyPath($formObject, implode('.', array_slice($propertySegments, 0, $i)));
311  $objectName .= '[' . $propertySegments[$i - 1] . ']';
312  $hiddenIdentityField = $this->renderHiddenIdentityField($object, $objectName);
313  // Add the hidden identity field to the ViewHelperVariableContainer
314  $additionalIdentityProperties = $this->viewHelperVariableContainer->get(
315  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties'
316  );
317  $additionalIdentityProperties[$objectName] = $hiddenIdentityField;
318  $this->viewHelperVariableContainer->addOrUpdate(
319  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties',
320  $additionalIdentityProperties
321  );
322  }
323  }
324 
330  protected function getPropertyValue()
331  {
332  if (!$this->viewHelperVariableContainer->exists(
333  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
334  )
335  ) {
336  return null;
337  }
338  $formObject = $this->viewHelperVariableContainer->get(
339  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
340  );
341  return ObjectAccess::getPropertyPath($formObject, $this->arguments['property']);
342  }
343 
349  protected function isObjectAccessorMode()
350  {
351  return $this->hasArgument('property') && $this->viewHelperVariableContainer->exists(
352  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
353  );
354  }
355 
361  protected function setErrorClassAttribute()
362  {
363  if ($this->hasArgument('class')) {
364  $cssClass = $this->arguments['class'] . ' ';
365  } else {
366  $cssClass = '';
367  }
368 
369  $mappingResultsForProperty = $this->getMappingResultsForProperty();
370  if ($mappingResultsForProperty->hasErrors()) {
371  if ($this->hasArgument('errorClass')) {
372  $cssClass .= $this->arguments['errorClass'];
373  } else {
374  $cssClass .= 'error';
375  }
376  $this->tag->addAttribute('class', $cssClass);
377  }
378  }
379 
385  protected function getMappingResultsForProperty()
386  {
387  if (!$this->isObjectAccessorMode()) {
388  return new \TYPO3\CMS\Extbase\Error\Result();
389  }
390  $originalRequestMappingResults = $this->getRequest()->getOriginalRequestMappingResults();
391  $formObjectName = $this->viewHelperVariableContainer->get(
392  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
393  );
394  return $originalRequestMappingResults->forProperty($formObjectName)->forProperty($this->arguments['property']);
395  }
396 
403  protected function renderHiddenFieldForEmptyValue()
404  {
405  $hiddenFieldNames = [];
406  if ($this->viewHelperVariableContainer->exists(
407  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
408  )
409  ) {
410  $hiddenFieldNames = $this->viewHelperVariableContainer->get(
411  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
412  );
413  }
414  $fieldName = $this->getName();
415  if (substr($fieldName, -2) === '[]') {
416  $fieldName = substr($fieldName, 0, -2);
417  }
418  if (!in_array($fieldName, $hiddenFieldNames)) {
419  $hiddenFieldNames[] = $fieldName;
420  $this->viewHelperVariableContainer->addOrUpdate(
421  \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields', $hiddenFieldNames
422  );
423  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="" />';
424  }
425  return '';
426  }
427 }
registerArgument($name, $type, $description, $required=false, $defaultValue=null)
static getPropertyPath($subject, $propertyPath)
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)