HtmlViewHelper.php 4.06 KB
Newer Older
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */

/**
 */

/**
 * Renders a string by passing it to a TYPO3 parseFunc.
 * You can either specify a path to the TypoScript setting or set the parseFunc options directly.
 * By default lib.parseFunc_RTE is used to parse the string.
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
23
 *
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
24
 * Example:
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
25
 *
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
26
 * (1) default parameters:
27
 * <f:format.html>foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
28
29
30
31
 *
 * Result:
 * <p class="bodytext">foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.</p>
 * (depending on your TYPO3 setup)
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
32
 *
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
33
 * (2) custom parseFunc
34
 * <f:format.html parseFuncTSPath="lib.parseFunc">foo <b>bar</b>. Some <LINK 1>link</LINK>.</f:format.html>
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
35
 *
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
36
37
 * Output:
 * foo <b>bar</b>. Some <a href="index.php?id=1" >link</a>.
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
38
 *
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
39
40
41
 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4198758
 *
 */
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
42
class Tx_Fluid_ViewHelpers_Format_HtmlViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
43
44
45
46
47
48

	/**
	 * @var	tslib_cObj
	 */
	protected $contentObject;

49
50
51
52
53
	/**
	 * @var	t3lib_fe contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
	 */
	protected $tsfeBackup;

Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
54
	/**
55
	 * If the escaping interceptor should be disabled inside this ViewHelper, then set this value to FALSE.
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
56
57
58
59
60
	 * This is internal and NO part of the API. It is very likely to change.
	 *
	 * @var boolean
	 * @internal
	 */
61
	protected $escapingInterceptorEnabled = FALSE;
Sebastian Kurfürst's avatar
Fluid:    
Sebastian Kurfürst committed
62

Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
63
64
65
66
67
68
	/**
	 * Constructor. Used to create an instance of tslib_cObj used by the render() method.
	 * @param tslib_cObj $contentObject injector for tslib_cObj (optional)
	 * @return void
	 */
	public function __construct($contentObject = NULL) {
69
		$this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
70
71
72
73
74
75
76
77
78
	}

	/**
	 * @param string $parseFuncTSPath path to TypoScript parseFunc setup.
	 * @return the parsed string.
	 * @author Bastian Waidelich <bastian@typo3.org>
	 * @author Niels Pardon <mail@niels-pardon.de>
	 */
	public function render($parseFuncTSPath = 'lib.parseFunc_RTE') {
79
80
81
82
		if (TYPO3_MODE === 'BE') {
			$this->simulateFrontendEnvironment();
		}

Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
83
		$value = $this->renderChildren();
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
		$content = $this->contentObject->parseFunc($value, array(), '< ' . $parseFuncTSPath);

		if (TYPO3_MODE === 'BE') {
			$this->resetFrontendEnvironment();
		}
		return $content;
	}

	/**
	 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
	 * This somewhat hacky work around is currently needed because the parseFunc() function of tslib_cObj relies on those variables to be set
	 *
	 * @return void
	 * @author Bastian Waidelich <bastian@typo3.org>
	 */
	protected function simulateFrontendEnvironment() {
		$this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
		$configurationManager = Tx_Extbase_Dispatcher::getConfigurationManager();
		$GLOBALS['TSFE'] = new stdClass();
		$GLOBALS['TSFE']->tmpl->setup = $configurationManager->loadTypoScriptSetup();
	}

	/**
	 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
	 *
	 * @return void
	 * @author Bastian Waidelich <bastian@typo3.org>
	 * @see simulateFrontendEnvironment()
	 */
	protected function resetFrontendEnvironment() {
		$GLOBALS['TSFE'] = $this->tsfeBackup;
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
115
116
117
118
	}
}

?>