[BUGFIX] add _loadRealInstance in f:form ViewHelper
[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 script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Abstract Form View Helper. Bundles functionality related to direct property access of objects in other Form ViewHelpers.
16 *
17 * If you set the "property" attribute to the name of the property to resolve from the object, this class will
18 * automatically set the name and value of a form element.
19 */
20 abstract class AbstractFormViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
24 * @inject
25 */
26 protected $persistenceManager;
27
28 /**
29 * Prefixes / namespaces the given name with the form field prefix
30 *
31 * @param string $fieldName field name to be prefixed
32 * @return string namespaced field name
33 */
34 protected function prefixFieldName($fieldName) {
35 if ($fieldName === NULL || $fieldName === '') {
36 return '';
37 }
38 if (!$this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')) {
39 return $fieldName;
40 }
41 $fieldNamePrefix = (string) $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
42 if ($fieldNamePrefix === '') {
43 return $fieldName;
44 }
45 $fieldNameSegments = explode('[', $fieldName, 2);
46 $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
47 if (count($fieldNameSegments) > 1) {
48 $fieldName .= '[' . $fieldNameSegments[1];
49 }
50 return $fieldName;
51 }
52
53 /**
54 * Renders a hidden form field containing the technical identity of the given object.
55 *
56 * @param object $object Object to create the identity field for
57 * @param string $name Name
58 * @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
59 * @see \TYPO3\CMS\Extbase\Mvc\Controller\Argument::setValue()
60 */
61 protected function renderHiddenIdentityField($object, $name) {
62 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
63 $object = $object->_loadRealInstance();
64 }
65 if (!is_object($object)
66 || !($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject)
67 || ($object->_isNew() && !$object->_isClone())) {
68 return '';
69 }
70 // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
71 // Using that one breaks re-submission of data in forms in case of an error.
72 $identifier = $object->getUid();
73 if ($identifier === NULL) {
74 return chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
75 }
76 $name = $this->prefixFieldName($name) . '[__identity]';
77 $this->registerFieldNameForFormTokenGeneration($name);
78
79 return chr(10) . '<input type="hidden" name="' . $name . '" value="' . $identifier . '" />' . chr(10);
80 }
81
82 /**
83 * Register a field name for inclusion in the HMAC / Form Token generation
84 *
85 * @param string $fieldName name of the field to register
86 * @return void
87 */
88 protected function registerFieldNameForFormTokenGeneration($fieldName) {
89 if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames')) {
90 $formFieldNames = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames');
91 } else {
92 $formFieldNames = array();
93 }
94 $formFieldNames[] = $fieldName;
95 $this->viewHelperVariableContainer->addOrUpdate('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames', $formFieldNames);
96 }
97 }