renderHiddenIdentityField()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 2
dl 0
loc 20
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
17
18
use TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject;
19
use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
20
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
21
use TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
23
24
/**
25
 * Abstract Form ViewHelper. Bundles functionality related to direct property access of objects in other Form ViewHelpers.
26
 *
27
 * If you set the "property" attribute to the name of the property to resolve from the object, this class will
28
 * automatically set the name and value of a form element.
29
 */
30
abstract class AbstractFormViewHelper extends AbstractTagBasedViewHelper
31
{
32
    /**
33
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
34
     */
35
    protected $persistenceManager;
36
37
    /**
38
     * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
39
     */
40
    public function injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
41
    {
42
        $this->persistenceManager = $persistenceManager;
43
    }
44
45
    /**
46
     * Prefixes / namespaces the given name with the form field prefix
47
     *
48
     * @param string $fieldName field name to be prefixed
49
     * @return string namespaced field name
50
     */
51
    protected function prefixFieldName($fieldName)
52
    {
53
        if ($fieldName === null || $fieldName === '') {
54
            return '';
55
        }
56
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
57
        if (!$viewHelperVariableContainer->exists(FormViewHelper::class, 'fieldNamePrefix')) {
58
            return $fieldName;
59
        }
60
        $fieldNamePrefix = (string)$viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix');
61
        if ($fieldNamePrefix === '') {
62
            return $fieldName;
63
        }
64
        $fieldNameSegments = explode('[', $fieldName, 2);
65
        $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
66
        if (count($fieldNameSegments) > 1) {
67
            $fieldName .= '[' . $fieldNameSegments[1];
68
        }
69
        return $fieldName;
70
    }
71
72
    /**
73
     * Renders a hidden form field containing the technical identity of the given object.
74
     *
75
     * @param object $object Object to create the identity field for
76
     * @param string $name Name
77
     * @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
78
     * @see \TYPO3\CMS\Extbase\Mvc\Controller\Argument::setValue()
79
     */
80
    protected function renderHiddenIdentityField($object, $name)
81
    {
82
        if ($object instanceof LazyLoadingProxy) {
83
            $object = $object->_loadRealInstance();
84
        }
85
        if (!is_object($object)
86
            || !($object instanceof AbstractDomainObject)
87
            || ($object->_isNew() && !$object->_isClone())) {
88
            return '';
89
        }
90
        // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
91
        // Using that one breaks re-submission of data in forms in case of an error.
92
        $identifier = $object->getUid();
93
        if ($identifier === null) {
94
            return LF . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . LF;
95
        }
96
        $name = $this->prefixFieldName($name) . '[__identity]';
97
        $this->registerFieldNameForFormTokenGeneration($name);
98
99
        return LF . '<input type="hidden" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars((string)$identifier) . '" />' . LF;
100
    }
101
102
    /**
103
     * Register a field name for inclusion in the HMAC / Form Token generation
104
     *
105
     * @param string $fieldName name of the field to register
106
     */
107
    protected function registerFieldNameForFormTokenGeneration($fieldName)
108
    {
109
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
110
        if ($viewHelperVariableContainer->exists(FormViewHelper::class, 'formFieldNames')) {
111
            $formFieldNames = $viewHelperVariableContainer->get(FormViewHelper::class, 'formFieldNames');
112
        } else {
113
            $formFieldNames = [];
114
        }
115
        $formFieldNames[] = $fieldName;
116
        $viewHelperVariableContainer->addOrUpdate(FormViewHelper::class, 'formFieldNames', $formFieldNames);
117
    }
118
}
119