[BUGFIX] Use late static binding for compilable viewhelpers
[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\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
20 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
22 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
23 use TYPO3\CMS\Extbase\Object\ObjectManager;
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 http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4198758
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 static protected $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 return static::renderStatic(
78 array(
79 'parseFuncTSPath' => $parseFuncTSPath,
80 ),
81 $this->buildRenderChildrenClosure(),
82 $this->renderingContext
83 );
84 }
85
86 /**
87 * @param array $arguments
88 * @param callable $renderChildrenClosure
89 * @param RenderingContextInterface $renderingContext
90 *
91 * @return string
92 */
93 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
94 $parseFuncTSPath = $arguments['parseFuncTSPath'];
95 if (TYPO3_MODE === 'BE') {
96 self::simulateFrontendEnvironment();
97 }
98 $value = $renderChildrenClosure();
99 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
100 $content = $contentObject->parseFunc($value, array(), '< ' . $parseFuncTSPath);
101 if (TYPO3_MODE === 'BE') {
102 self::resetFrontendEnvironment();
103 }
104 return $content;
105 }
106
107 /**
108 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
109 * 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
110 *
111 * @return void
112 */
113 static protected function simulateFrontendEnvironment() {
114 self::$tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
115 $GLOBALS['TSFE'] = new \stdClass();
116 $GLOBALS['TSFE']->tmpl = new \stdClass();
117 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
118 $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
119 $GLOBALS['TSFE']->tmpl->setup = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
120 }
121
122 /**
123 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
124 *
125 * @return void
126 * @see simulateFrontendEnvironment()
127 */
128 static protected function resetFrontendEnvironment() {
129 $GLOBALS['TSFE'] = self::$tsfeBackup;
130 }
131
132 }