6bc05bcb415b78447f595c3a1193580457af428d
[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 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 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
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
57 {
58 use CompileWithRenderStatic;
59
60 /**
61 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
62 */
63 protected static $tsfeBackup;
64
65 /**
66 * Children must not be escaped, to be able to pass {bodytext} directly to it
67 *
68 * @var bool
69 */
70 protected $escapeChildren = false;
71
72 /**
73 * Plain HTML should be returned, no output escaping allowed
74 *
75 * @var bool
76 */
77 protected $escapeOutput = false;
78
79 /**
80 * Initialize arguments.
81 *
82 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
83 */
84 public function initializeArguments()
85 {
86 $this->registerArgument('parseFuncTSPath', 'string', ' path to TypoScript parseFunc setup.', false, 'lib.parseFunc_RTE');
87 }
88
89 /**
90 * @param array $arguments
91 * @param \Closure $renderChildrenClosure
92 * @param RenderingContextInterface $renderingContext
93 *
94 * @return string the parsed string.
95 */
96 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
97 {
98 $parseFuncTSPath = $arguments['parseFuncTSPath'];
99 if (TYPO3_MODE === 'BE') {
100 self::simulateFrontendEnvironment();
101 }
102 $value = $renderChildrenClosure();
103 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
104 $contentObject->start([]);
105 $content = $contentObject->parseFunc($value, [], '< ' . $parseFuncTSPath);
106 if (TYPO3_MODE === 'BE') {
107 self::resetFrontendEnvironment();
108 }
109 return $content;
110 }
111
112 /**
113 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
114 * 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
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 * @see simulateFrontendEnvironment()
130 */
131 protected static function resetFrontendEnvironment()
132 {
133 $GLOBALS['TSFE'] = self::$tsfeBackup;
134 }
135 }