e7def510de19ec523f6966ab0bad3e5ad803abd9
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / CObjectViewHelper.php
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\Utility\GeneralUtility;
18
19 /**
20 * This ViewHelper renders CObjects from the global TypoScript configuration.
21 *
22 * = Examples =
23 *
24 * <code title="Render lib object">
25 * <f:cObject typoscriptObjectPath="lib.someLibObject" />
26 * </code>
27 * <output>
28 * rendered lib.someLibObject
29 * </output>
30 *
31 * <code title="Specify cObject data & current value">
32 * <f:cObject typoscriptObjectPath="lib.customHeader" data="{article}" currentValueKey="title" />
33 * </code>
34 * <output>
35 * rendered lib.customHeader. data and current value will be available in TypoScript
36 * </output>
37 *
38 * <code title="inline notation">
39 * {article -> f:cObject(typoscriptObjectPath: 'lib.customHeader')}
40 * </code>
41 * <output>
42 * rendered lib.customHeader. data will be available in TypoScript
43 * </output>
44 */
45 class CObjectViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
46 {
47 /**
48 * Disable escaping of child nodes' output
49 *
50 * @var bool
51 */
52 protected $escapeChildren = false;
53
54 /**
55 * Disable escaping of this node's output
56 *
57 * @var bool
58 */
59 protected $escapeOutput = false;
60
61 /**
62 * @var array
63 */
64 protected $typoScriptSetup;
65
66 /**
67 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
68 */
69 protected $tsfeBackup;
70
71 /**
72 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
73 */
74 protected $configurationManager;
75
76 /**
77 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
78 * @return void
79 */
80 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
81 {
82 $this->configurationManager = $configurationManager;
83 $this->typoScriptSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
84 }
85
86 /**
87 * Renders the TypoScript object in the given TypoScript setup path.
88 *
89 * @param string $typoscriptObjectPath the TypoScript setup path of the TypoScript object to render
90 * @param mixed $data 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
91 * @param string $currentValueKey
92 * @param string $table
93 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
94 * @return string the content of the rendered TypoScript object
95 */
96 public function render($typoscriptObjectPath, $data = null, $currentValueKey = null, $table = '')
97 {
98 if (TYPO3_MODE === 'BE') {
99 $this->simulateFrontendEnvironment();
100 }
101 if ($data === null) {
102 $data = $this->renderChildren();
103 }
104 $currentValue = null;
105 if (is_object($data)) {
106 $data = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($data);
107 } elseif (is_string($data) || is_numeric($data)) {
108 $currentValue = (string)$data;
109 $data = array($data);
110 }
111 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject */
112 $contentObject = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
113 $contentObject->start($data, $table);
114 if ($currentValue !== null) {
115 $contentObject->setCurrentVal($currentValue);
116 } elseif ($currentValueKey !== null && isset($data[$currentValueKey])) {
117 $contentObject->setCurrentVal($data[$currentValueKey]);
118 }
119 $pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
120 $lastSegment = array_pop($pathSegments);
121 $setup = $this->typoScriptSetup;
122 foreach ($pathSegments as $segment) {
123 if (!array_key_exists(($segment . '.'), $setup)) {
124 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('TypoScript object path "' . htmlspecialchars($typoscriptObjectPath) . '" does not exist', 1253191023);
125 }
126 $setup = $setup[$segment . '.'];
127 }
128 $content = $contentObject->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.']);
129 if (TYPO3_MODE === 'BE') {
130 $this->resetFrontendEnvironment();
131 }
132 return $content;
133 }
134
135 /**
136 * Sets the $TSFE->cObjectDepthCounter in Backend mode
137 * This somewhat hacky work around is currently needed because the cObjGetSingle() function of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on this setting
138 *
139 * @return void
140 */
141 protected function simulateFrontendEnvironment()
142 {
143 $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : null;
144 $GLOBALS['TSFE'] = new \stdClass();
145 $GLOBALS['TSFE']->cObjectDepthCounter = 100;
146 }
147
148 /**
149 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
150 *
151 * @return void
152 * @see simulateFrontendEnvironment()
153 */
154 protected function resetFrontendEnvironment()
155 {
156 $GLOBALS['TSFE'] = $this->tsfeBackup;
157 }
158 }