[FOLLOWUP][BUGFIX] Use Fluid standalone abstract classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / AbstractFormViewHelper.php
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
17 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
18
19 /**
20 * Abstract Form View Helper. Bundles functionality related to direct property access of objects in other Form ViewHelpers.
21 *
22 * If you set the "property" attribute to the name of the property to resolve from the object, this class will
23 * automatically set the name and value of a form element.
24 */
25 abstract class AbstractFormViewHelper extends AbstractTagBasedViewHelper
26 {
27 /**
28 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
29 */
30 protected $persistenceManager;
31
32 /**
33 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
34 */
35 public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
36 {
37 $this->persistenceManager = $persistenceManager;
38 }
39
40 /**
41 * Prefixes / namespaces the given name with the form field prefix
42 *
43 * @param string $fieldName field name to be prefixed
44 * @return string namespaced field name
45 */
46 protected function prefixFieldName($fieldName)
47 {
48 if ($fieldName === null || $fieldName === '') {
49 return '';
50 }
51 if (!$this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix')) {
52 return $fieldName;
53 }
54 $fieldNamePrefix = (string)$this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
55 if ($fieldNamePrefix === '') {
56 return $fieldName;
57 }
58 $fieldNameSegments = explode('[', $fieldName, 2);
59 $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
60 if (count($fieldNameSegments) > 1) {
61 $fieldName .= '[' . $fieldNameSegments[1];
62 }
63 return $fieldName;
64 }
65
66 /**
67 * Renders a hidden form field containing the technical identity of the given object.
68 *
69 * @param object $object Object to create the identity field for
70 * @param string $name Name
71 * @return string A hidden field containing the Identity (UID in TYPO3 Flow, uid in Extbase) of the given object or NULL if the object is unknown to the persistence framework
72 * @see \TYPO3\CMS\Extbase\Mvc\Controller\Argument::setValue()
73 */
74 protected function renderHiddenIdentityField($object, $name)
75 {
76 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
77 $object = $object->_loadRealInstance();
78 }
79 if (!is_object($object)
80 || !($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject)
81 || ($object->_isNew() && !$object->_isClone())) {
82 return '';
83 }
84 // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
85 // Using that one breaks re-submission of data in forms in case of an error.
86 $identifier = $object->getUid();
87 if ($identifier === null) {
88 return LF . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . LF;
89 }
90 $name = $this->prefixFieldName($name) . '[__identity]';
91 $this->registerFieldNameForFormTokenGeneration($name);
92
93 return LF . '<input type="hidden" name="' . $name . '" value="' . $identifier . '" />' . LF;
94 }
95
96 /**
97 * Register a field name for inclusion in the HMAC / Form Token generation
98 *
99 * @param string $fieldName name of the field to register
100 */
101 protected function registerFieldNameForFormTokenGeneration($fieldName)
102 {
103 if ($this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames')) {
104 $formFieldNames = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames');
105 } else {
106 $formFieldNames = [];
107 }
108 $formFieldNames[] = $fieldName;
109 $this->viewHelperVariableContainer->addOrUpdate(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames', $formFieldNames);
110 }
111 }