[~TASK] Fluid (ViewHelpers): Fixed BE support of the ViewHelpers cObject, format...
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlViewHelper.php
1 <?php
2
3 /* *
4 * This script is part of the TYPO3 project - inspiring people to share! *
5 * *
6 * TYPO3 is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License version 2 as published by *
8 * the Free Software Foundation. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
13 * Public License for more details. *
14 * */
15
16 /**
17 */
18
19 /**
20 * Renders a string by passing it to a TYPO3 parseFunc.
21 * You can either specify a path to the TypoScript setting or set the parseFunc options directly.
22 * By default lib.parseFunc_RTE is used to parse the string.
23 *
24 * Example:
25 *
26 * (1) default parameters:
27 * <f:format.html>foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
28 *
29 * Result:
30 * <p class="bodytext">foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.</p>
31 * (depending on your TYPO3 setup)
32 *
33 * (2) custom parseFunc
34 * <f:format.html parseFuncTSPath="lib.parseFunc">foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
35 *
36 * Output:
37 * foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.
38 *
39 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4198758
40 *
41 */
42 class Tx_Fluid_ViewHelpers_Format_HtmlViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
43
44 /**
45 * @var tslib_cObj
46 */
47 protected $contentObject;
48
49 /**
50 * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
51 */
52 protected $tsfeBackup;
53
54 /**
55 * If the escaping interceptor should be disabled inside this ViewHelper, then set this value to FALSE.
56 * This is internal and NO part of the API. It is very likely to change.
57 *
58 * @var boolean
59 * @internal
60 */
61 protected $escapingInterceptorEnabled = FALSE;
62
63 /**
64 * Constructor. Used to create an instance of tslib_cObj used by the render() method.
65 * @param tslib_cObj $contentObject injector for tslib_cObj (optional)
66 * @return void
67 */
68 public function __construct($contentObject = NULL) {
69 $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
70 }
71
72 /**
73 * @param string $parseFuncTSPath path to TypoScript parseFunc setup.
74 * @return the parsed string.
75 * @author Bastian Waidelich <bastian@typo3.org>
76 * @author Niels Pardon <mail@niels-pardon.de>
77 */
78 public function render($parseFuncTSPath = 'lib.parseFunc_RTE') {
79 if (TYPO3_MODE === 'BE') {
80 $this->simulateFrontendEnvironment();
81 }
82
83 $value = $this->renderChildren();
84 $content = $this->contentObject->parseFunc($value, array(), '< ' . $parseFuncTSPath);
85
86 if (TYPO3_MODE === 'BE') {
87 $this->resetFrontendEnvironment();
88 }
89 return $content;
90 }
91
92 /**
93 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
94 * This somewhat hacky work around is currently needed because the parseFunc() function of tslib_cObj relies on those variables to be set
95 *
96 * @return void
97 * @author Bastian Waidelich <bastian@typo3.org>
98 */
99 protected function simulateFrontendEnvironment() {
100 $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
101 $configurationManager = Tx_Extbase_Dispatcher::getConfigurationManager();
102 $GLOBALS['TSFE'] = new stdClass();
103 $GLOBALS['TSFE']->tmpl->setup = $configurationManager->loadTypoScriptSetup();
104 }
105
106 /**
107 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
108 *
109 * @return void
110 * @author Bastian Waidelich <bastian@typo3.org>
111 * @see simulateFrontendEnvironment()
112 */
113 protected function resetFrontendEnvironment() {
114 $GLOBALS['TSFE'] = $this->tsfeBackup;
115 }
116 }
117
118 ?>