‪TYPO3CMS  ‪main
AbstractFormViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
25 
34 abstract class ‪AbstractFormViewHelper extends AbstractTagBasedViewHelper
35 {
37 
39  {
40  $this->persistenceManager = ‪$persistenceManager;
41  }
42 
46  protected function ‪prefixFieldName(string $fieldName): string
47  {
48  if ($fieldName === '') {
49  return '';
50  }
51  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
52  if (!$viewHelperVariableContainer->exists(FormViewHelper::class, 'fieldNamePrefix')) {
53  return $fieldName;
54  }
55  $fieldNamePrefix = (string)$viewHelperVariableContainer->get(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 $object, ?string $name): string
76  {
77  if ($object instanceof ‪LazyLoadingProxy) {
78  $object = $object->_loadRealInstance();
79  }
80  if (!is_object($object)
81  || !($object instanceof ‪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((string)‪$identifier) . '" />' . LF;
95  }
96 
100  protected function ‪registerFieldNameForFormTokenGeneration(string $fieldName): void
101  {
102  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
103  if ($viewHelperVariableContainer->exists(FormViewHelper::class, 'formFieldNames')) {
104  $formFieldNames = $viewHelperVariableContainer->get(FormViewHelper::class, 'formFieldNames');
105  } else {
106  $formFieldNames = [];
107  }
108  $formFieldNames[] = $fieldName;
109  $viewHelperVariableContainer->addOrUpdate(FormViewHelper::class, 'formFieldNames', $formFieldNames);
110  }
111 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: AbstractFormViewHelper.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: AbstractFormViewHelper.php:36
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration(string $fieldName)
Definition: AbstractFormViewHelper.php:100
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper
Definition: FormViewHelper.php:72
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject
Definition: AbstractDomainObject.php:31
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\renderHiddenIdentityField
‪string renderHiddenIdentityField(?object $object, ?string $name)
Definition: AbstractFormViewHelper.php:75
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper
Definition: AbstractFormViewHelper.php:35
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\prefixFieldName
‪prefixFieldName(string $fieldName)
Definition: AbstractFormViewHelper.php:46
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37