687ea01c43e4f249a235b7f2c13e6b4d8b5d0016
[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\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23
24 /**
25 * Renders a string by passing it to a TYPO3 parseFunc.
26 * You can either specify a path to the TypoScript setting or set the parseFunc options directly.
27 * By default lib.parseFunc_RTE is used to parse the string.
28 *
29 * == Examples ==
30 *
31 * <code title="Default parameters">
32 * <f:format.html>foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
33 * </code>
34 * <output>
35 * <p class="bodytext">foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.</p>
36 * (depending on your TYPO3 setup)
37 * </output>
38 *
39 * <code title="Custom parseFunc">
40 * <f:format.html parseFuncTSPath="lib.parseFunc">foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
41 * </code>
42 * <output>
43 * foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.
44 * </output>
45 *
46 * <code title="Inline notation">
47 * {someText -> f:format.html(parseFuncTSPath: 'lib.parseFunc')}
48 * </code>
49 * <output>
50 * foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.
51 * </output>
52 *
53 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Parsefunc/
54 */
55 class HtmlViewHelper extends AbstractViewHelper
56 {
57 /**
58 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
59 */
60 protected static $tsfeBackup;
61
62 /**
63 * @var bool
64 */
65 protected $escapeChildren = false;
66
67 /**
68 * @var bool
69 */
70 protected $escapeOutput = 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 array(
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 GeneralUtility::deprecationLog('The usage of the viewhelper "f:format.html" in the backend context is discouraged, and will stop working with CMS 9. '
99 . 'Use the viewhelper "f:format.raw" instead.');
100 self::simulateFrontendEnvironment();
101 }
102 $value = $renderChildrenClosure();
103 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
104 $content = $contentObject->parseFunc($value, array(), '< ' . $parseFuncTSPath);
105 if (TYPO3_MODE === 'BE') {
106 self::resetFrontendEnvironment();
107 }
108 return $content;
109 }
110
111 /**
112 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
113 * 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
114 *
115 * @return void
116 */
117 protected static function simulateFrontendEnvironment()
118 {
119 self::$tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : null;
120 $GLOBALS['TSFE'] = new \stdClass();
121 $GLOBALS['TSFE']->tmpl = new \stdClass();
122 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
123 $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
124 $GLOBALS['TSFE']->tmpl->setup = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
125 }
126
127 /**
128 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
129 *
130 * @return void
131 * @see simulateFrontendEnvironment()
132 */
133 protected static function resetFrontendEnvironment()
134 {
135 $GLOBALS['TSFE'] = self::$tsfeBackup;
136 }
137 }