‪TYPO3CMS  9.5
AbstractFormViewHelper.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 
17 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
18 
25 abstract class ‪AbstractFormViewHelper extends AbstractTagBasedViewHelper
26 {
30  protected ‪$persistenceManager;
31 
35  public function ‪injectPersistenceManager(\‪TYPO3\CMS\‪Extbase\Persistence\PersistenceManagerInterface ‪$persistenceManager)
36  {
37  $this->persistenceManager = ‪$persistenceManager;
38  }
39 
46  protected function ‪prefixFieldName($fieldName)
47  {
48  if ($fieldName === null || $fieldName === '') {
49  return '';
50  }
51  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
52  if (!$viewHelperVariableContainer->exists(\‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix')) {
53  return $fieldName;
54  }
55  $fieldNamePrefix = (string)$viewHelperVariableContainer->get(\‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
56  if ($fieldNamePrefix === '') {
57  return $fieldName;
58  }
59  $fieldNameSegments = explode('[', $fieldName, 2);
60  $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
61  if (count($fieldNameSegments) > 1) {
62  $fieldName .= '[' . $fieldNameSegments[1];
63  }
64  return $fieldName;
65  }
66 
75  protected function ‪renderHiddenIdentityField($object, $name)
76  {
77  if ($object instanceof \‪TYPO3\CMS\‪Extbase\Persistence\Generic\LazyLoadingProxy) {
78  $object = $object->_loadRealInstance();
79  }
80  if (!is_object($object)
81  || !($object instanceof \‪TYPO3\CMS\‪Extbase\DomainObject\AbstractDomainObject)
82  || ($object->_isNew() && !$object->_isClone())) {
83  return '';
84  }
85  // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
86  // Using that one breaks re-submission of data in forms in case of an error.
87  $identifier = $object->getUid();
88  if ($identifier === null) {
89  return LF . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . LF;
90  }
91  $name = $this->‪prefixFieldName($name) . '[__identity]';
93 
94  return LF . '<input type="hidden" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($identifier) . '" />' . LF;
95  }
96 
102  protected function ‪registerFieldNameForFormTokenGeneration($fieldName)
103  {
104  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
105  if ($viewHelperVariableContainer->exists(\‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames')) {
106  $formFieldNames = $viewHelperVariableContainer->get(\‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames');
107  } else {
108  $formFieldNames = [];
109  }
110  $formFieldNames[] = $fieldName;
111  $viewHelperVariableContainer->addOrUpdate(\‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames', $formFieldNames);
112  }
113 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: AbstractFormViewHelper.php:29
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\renderHiddenIdentityField
‪string renderHiddenIdentityField($object, $name)
Definition: AbstractFormViewHelper.php:74
‪TYPO3
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\injectPersistenceManager
‪injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: AbstractFormViewHelper.php:34
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:101
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper
Definition: AbstractFormViewHelper.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\prefixFieldName
‪string prefixFieldName($fieldName)
Definition: AbstractFormViewHelper.php:45