Completed
Push — master ( 04f394...fd043c )
by
unknown
17:50
created

CObjectViewHelper::renderContentObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 4
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace TYPO3\CMS\Fluid\ViewHelpers;
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 TYPO3\CMS\Core\Context\Context;
18
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
21
use TYPO3\CMS\Extbase\Object\ObjectManager;
22
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
26
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
27
28
/**
29
 * This ViewHelper renders CObjects from the global TypoScript configuration.
30
 *
31
 * .. note::
32
 *    You have to ensure proper escaping (htmlspecialchars/intval/etc.) on your own!
33
 *
34
 * Examples
35
 * ========
36
 *
37
 * Render lib object
38
 * -----------------
39
 *
40
 * ::
41
 *
42
 *    <f:cObject typoscriptObjectPath="lib.someLibObject" />
43
 *
44
 * Rendered :ts:`lib.someLibObject`.
45
 *
46
 * Specify cObject data & current value
47
 * ------------------------------------
48
 *
49
 * ::
50
 *
51
 *    <f:cObject typoscriptObjectPath="lib.customHeader" data="{article}" currentValueKey="title" />
52
 *
53
 * Rendered :ts:`lib.customHeader`. Data and current value will be available in TypoScript.
54
 *
55
 * Inline notation
56
 * ---------------
57
 *
58
 * ::
59
 *
60
 *    {article -> f:cObject(typoscriptObjectPath: 'lib.customHeader')}
61
 *
62
 * Rendered :ts:`lib.customHeader`. Data will be available in TypoScript.
63
 *
64
 * Accessing the data in TypoScript
65
 * --------------------------------
66
 *
67
 * ::
68
 *
69
 *    lib.customHeader = COA
70
 *    lib.customHeader {
71
 *        10 = TEXT
72
 *        10.field = author
73
 *        20 = TEXT
74
 *        20.current = 1
75
 *    }
76
 *
77
 * When passing an object with ``{data}`` the properties of the object are accessable with :ts:`.field` in
78
 * TypoScript. If only a single value is passed or the ``currentValueKey`` is specified :ts:`.current = 1`
79
 * can be used in the TypoScript.
80
 */
81
class CObjectViewHelper extends AbstractViewHelper
82
{
83
    use CompileWithContentArgumentAndRenderStatic;
84
85
    /**
86
     * Disable escaping of child nodes' output
87
     *
88
     * @var bool
89
     */
90
    protected $escapeChildren = false;
91
92
    /**
93
     * Disable escaping of this node's output
94
     *
95
     * @var bool
96
     */
97
    protected $escapeOutput = false;
98
99
    /**
100
     * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
101
     */
102
    protected static $tsfeBackup;
103
104
    /**
105
     * Initialize arguments.
106
     *
107
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
108
     */
109
    public function initializeArguments()
110
    {
111
        $this->registerArgument('data', 'mixed', 'the data to be used for rendering the cObject. Can be an object, array or string. If this argument is not set, child nodes will be used');
112
        $this->registerArgument('typoscriptObjectPath', 'string', 'the TypoScript setup path of the TypoScript object to render', true);
113
        $this->registerArgument('currentValueKey', 'string', 'currentValueKey');
114
        $this->registerArgument('table', 'string', 'the table name associated with "data" argument. Typically tt_content or one of your custom tables. This argument should be set if rendering a FILES cObject where file references are used, or if the data argument is a database record.', false, '');
115
    }
116
117
    /**
118
     * Renders the TypoScript object in the given TypoScript setup path.
119
     *
120
     * @param array $arguments
121
     * @param \Closure $renderChildrenClosure
122
     * @param RenderingContextInterface $renderingContext
123
     * @return mixed
124
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
125
     */
126
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
127
    {
128
        $data = $renderChildrenClosure();
129
        $typoscriptObjectPath = $arguments['typoscriptObjectPath'];
130
        $currentValueKey = $arguments['currentValueKey'];
131
        $table = $arguments['table'];
132
        $contentObjectRenderer = static::getContentObjectRenderer();
133
        if (!isset($GLOBALS['TSFE']) || !($GLOBALS['TSFE'] instanceof TypoScriptFrontendController)) {
134
            static::simulateFrontendEnvironment();
135
        }
136
        $currentValue = null;
137
        if (is_object($data)) {
138
            $data = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($data);
139
        } elseif (is_string($data) || is_numeric($data)) {
140
            $currentValue = (string)$data;
141
            $data = [$data];
142
        }
143
        $contentObjectRenderer->start($data, $table);
144
        if ($currentValue !== null) {
145
            $contentObjectRenderer->setCurrentVal($currentValue);
146
        } elseif ($currentValueKey !== null && isset($data[$currentValueKey])) {
147
            $contentObjectRenderer->setCurrentVal($data[$currentValueKey]);
148
        }
149
        $pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
150
        $lastSegment = array_pop($pathSegments);
151
        $setup = static::getConfigurationManager()->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
152
        foreach ($pathSegments as $segment) {
153
            if (!array_key_exists($segment . '.', $setup)) {
154
                throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception(
155
                    'TypoScript object path "' . $typoscriptObjectPath . '" does not exist',
156
                    1253191023
157
                );
158
            }
159
            $setup = $setup[$segment . '.'];
160
        }
161
        if (!isset($setup[$lastSegment])) {
162
            throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception(
163
                'No Content Object definition found at TypoScript object path "' . $typoscriptObjectPath . '"',
164
                1540246570
165
            );
166
        }
167
        $content = self::renderContentObject($contentObjectRenderer, $setup, $typoscriptObjectPath, $lastSegment);
168
        if (!isset($GLOBALS['TSFE']) || !($GLOBALS['TSFE'] instanceof TypoScriptFrontendController)) {
169
            static::resetFrontendEnvironment();
170
        }
171
        return $content;
172
    }
173
174
    /**
175
     * Renders single content object and increases time tracker stack pointer
176
     *
177
     * @param ContentObjectRenderer $contentObjectRenderer
178
     * @param array $setup
179
     * @param string $typoscriptObjectPath
180
     * @param string $lastSegment
181
     * @return string
182
     */
183
    protected static function renderContentObject(ContentObjectRenderer $contentObjectRenderer, array $setup, string $typoscriptObjectPath, string $lastSegment): string
184
    {
185
        $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
186
        if ($timeTracker->LR) {
187
            $timeTracker->push('/f:cObject/', '<' . $typoscriptObjectPath);
188
        }
189
        $timeTracker->incStackPointer();
190
        $content = $contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.'] ?? [], $typoscriptObjectPath);
191
        $timeTracker->decStackPointer();
192
        if ($timeTracker->LR) {
193
            $timeTracker->pull($content);
194
        }
195
        return $content;
196
    }
197
198
    /**
199
     * @return ConfigurationManagerInterface
200
     */
201
    protected static function getConfigurationManager()
202
    {
203
        return GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationManagerInterface::class);
204
    }
205
206
    /**
207
     * @return ContentObjectRenderer
208
     */
209
    protected static function getContentObjectRenderer()
210
    {
211
        return GeneralUtility::makeInstance(
212
            ContentObjectRenderer::class,
213
            $GLOBALS['TSFE'] ?? GeneralUtility::makeInstance(TypoScriptFrontendController::class, GeneralUtility::makeInstance(Context::class))
0 ignored issues
show
Bug introduced by
TYPO3\CMS\Core\Utility\G...Context\Context::class) of type object is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
            $GLOBALS['TSFE'] ?? GeneralUtility::makeInstance(TypoScriptFrontendController::class, /** @scrutinizer ignore-type */ GeneralUtility::makeInstance(Context::class))
Loading history...
214
        );
215
    }
216
217
    /**
218
     * Sets the $TSFE->cObjectDepthCounter in Backend mode
219
     * This somewhat hacky work around is currently needed because the cObjGetSingle() function of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on this setting
220
     */
221
    protected static function simulateFrontendEnvironment()
222
    {
223
        static::$tsfeBackup = $GLOBALS['TSFE'] ?? null;
224
        $GLOBALS['TSFE'] = new \stdClass();
225
        $GLOBALS['TSFE']->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
226
        $GLOBALS['TSFE']->cObjectDepthCounter = 100;
227
    }
228
229
    /**
230
     * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
231
     *
232
     * @see simulateFrontendEnvironment()
233
     */
234
    protected static function resetFrontendEnvironment()
235
    {
236
        $GLOBALS['TSFE'] = static::$tsfeBackup;
237
    }
238
}
239