TYPO3 CMS  TYPO3_8-7
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 
24 {
29 
33  public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
34  {
35  $this->persistenceManager = $persistenceManager;
36  }
37 
44  protected function prefixFieldName($fieldName)
45  {
46  if ($fieldName === null || $fieldName === '') {
47  return '';
48  }
49  if (!$this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix')) {
50  return $fieldName;
51  }
52  $fieldNamePrefix = (string)$this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
53  if ($fieldNamePrefix === '') {
54  return $fieldName;
55  }
56  $fieldNameSegments = explode('[', $fieldName, 2);
57  $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
58  if (count($fieldNameSegments) > 1) {
59  $fieldName .= '[' . $fieldNameSegments[1];
60  }
61  return $fieldName;
62  }
63 
72  protected function renderHiddenIdentityField($object, $name)
73  {
74  if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
75  $object = $object->_loadRealInstance();
76  }
77  if (!is_object($object)
78  || !($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject)
79  || ($object->_isNew() && !$object->_isClone())) {
80  return '';
81  }
82  // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
83  // Using that one breaks re-submission of data in forms in case of an error.
84  $identifier = $object->getUid();
85  if ($identifier === null) {
86  return LF . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . LF;
87  }
88  $name = $this->prefixFieldName($name) . '[__identity]';
90 
91  return LF . '<input type="hidden" name="' . $name . '" value="' . $identifier . '" />' . LF;
92  }
93 
99  protected function registerFieldNameForFormTokenGeneration($fieldName)
100  {
101  if ($this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames')) {
102  $formFieldNames = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames');
103  } else {
104  $formFieldNames = [];
105  }
106  $formFieldNames[] = $fieldName;
107  $this->viewHelperVariableContainer->addOrUpdate(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames', $formFieldNames);
108  }
109 }
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)