[BUGFIX] Fully initialize ContentObjectRenderer in HtmlViewHelper
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
23 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
24
25 /**
26 * Renders a string by passing it to a TYPO3 parseFunc.
27 * You can either specify a path to the TypoScript setting or set the parseFunc options directly.
28 * By default lib.parseFunc_RTE is used to parse the string.
29 *
30 * == Examples ==
31 *
32 * <code title="Default parameters">
33 * <f:format.html>foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
34 * </code>
35 * <output>
36 * <p class="bodytext">foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.</p>
37 * (depending on your TYPO3 setup)
38 * </output>
39 *
40 * <code title="Custom parseFunc">
41 * <f:format.html parseFuncTSPath="lib.parseFunc">foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
42 * </code>
43 * <output>
44 * foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.
45 * </output>
46 *
47 * <code title="Inline notation">
48 * {someText -> f:format.html(parseFuncTSPath: 'lib.parseFunc')}
49 * </code>
50 * <output>
51 * foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.
52 * </output>
53 *
54 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Parsefunc/
55 */
56 class HtmlViewHelper extends AbstractViewHelper implements CompilableInterface
57 {
58 /**
59 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
60 */
61 protected static $tsfeBackup;
62
63 /**
64 * If the escaping interceptor should be disabled inside this ViewHelper, then set this value to FALSE.
65 * This is internal and NO part of the API. It is very likely to change.
66 *
67 * @var bool
68 * @internal
69 */
70 protected $escapingInterceptorEnabled = false;
71
72 /**
73 * @param string $parseFuncTSPath path to TypoScript parseFunc setup.
74 * @return string the parsed string.
75 */
76 public function render($parseFuncTSPath = 'lib.parseFunc_RTE')
77 {
78 return static::renderStatic(
79 [
80 'parseFuncTSPath' => $parseFuncTSPath,
81 ],
82 $this->buildRenderChildrenClosure(),
83 $this->renderingContext
84 );
85 }
86
87 /**
88 * @param array $arguments
89 * @param callable $renderChildrenClosure
90 * @param RenderingContextInterface $renderingContext
91 *
92 * @return string
93 */
94 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
95 {
96 $parseFuncTSPath = $arguments['parseFuncTSPath'];
97 if (TYPO3_MODE === 'BE') {
98 self::simulateFrontendEnvironment();
99 }
100 $value = $renderChildrenClosure();
101 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
102 $contentObject->start([]);
103 $content = $contentObject->parseFunc($value, [], '< ' . $parseFuncTSPath);
104 if (TYPO3_MODE === 'BE') {
105 self::resetFrontendEnvironment();
106 }
107 return $content;
108 }
109
110 /**
111 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
112 * This somewhat hacky work around is currently needed because the parseFunc() function of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on those variables to be set
113 *
114 * @return void
115 */
116 protected static function simulateFrontendEnvironment()
117 {
118 self::$tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : null;
119 $GLOBALS['TSFE'] = new \stdClass();
120 $GLOBALS['TSFE']->tmpl = new \stdClass();
121 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
122 $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
123 $GLOBALS['TSFE']->tmpl->setup = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
124 }
125
126 /**
127 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
128 *
129 * @return void
130 * @see simulateFrontendEnvironment()
131 */
132 protected static function resetFrontendEnvironment()
133 {
134 $GLOBALS['TSFE'] = self::$tsfeBackup;
135 }
136 }