[BUGFIX] FLUIDTEMPLATE cObj uses wrong template file
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / FluidTemplateContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Xavier Perseguers <typo3@perseguers.ch>
8 * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 /**
32 * Contains TEMPLATE class object.
33 *
34 * @author Xavier Perseguers <typo3@perseguers.ch>
35 * @author Steffen Kamper <steffen@typo3.org>
36 * @author Bastian Waidelich <bastian@typo3.org>
37 * @author Steffen Ritter <info@steffen-ritter.net>
38 * @author Benjamin Mack <benni@typo3.org>
39 */
40 class FluidTemplateContentObject extends \TYPO3\CMS\Frontend\ContentObject\AbstractContentObject {
41
42 /**
43 * @var \TYPO3\CMS\Fluid\View\StandaloneView
44 */
45 protected $view = NULL;
46
47 public function __construct(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer) {
48 parent::__construct($contentObjectRenderer);
49 }
50
51 /**
52 * Rendering the cObject, FLUIDTEMPLATE
53 *
54 * Configuration properties:
55 * - file string+stdWrap The FLUID template file
56 * - layoutRootPath filepath+stdWrap Root path to layouts
57 * - partialRootPath filepath+stdWrap Root path to partial
58 * - variable array of cObjects, the keys are the variable names in fluid
59 * - extbase.pluginName
60 * - extbase.controllerExtensionName
61 * - extbase.controllerName
62 * - extbase.controllerActionName
63 *
64 * Example:
65 * 10 = FLUIDTEMPLATE
66 * 10.file = fileadmin/templates/mytemplate.html
67 * 10.partialRootPath = fileadmin/templates/partial/
68 * 10.variables {
69 * mylabel = TEXT
70 * mylabel.value = Label from TypoScript coming
71 * }
72 *
73 * @param array $conf Array of TypoScript properties
74 * @return string The HTML output
75 */
76 public function render($conf = array()) {
77 $this->initializeStandaloneViewInstance();
78
79 if (!is_array($conf)) {
80 $conf = array();
81 }
82
83 $this->setTemplate($conf);
84 $this->setLayoutRootPath($conf);
85 $this->setPartialRootPath($conf);
86 $this->setFormat($conf);
87 $this->setExtbaseVariables($conf);
88 $this->assignContentObjectVariables($conf);
89 $this->assignContentObjectDataAndCurrent($conf);
90
91 $content = $this->renderFluidView();
92
93 return $this->applyStandardWrapToRenderedContent($content, $conf);
94 }
95
96 /**
97 * Creating standalone view instance must not be done in construct() as
98 * it can lead to a nasty cache issue since content object instances
99 * are not always re-created by the content object rendered for every
100 * usage, but can be re-used. Thus, we need a fresh instance of
101 * StandaloneView every time render() is called.
102 *
103 * @return void
104 */
105 protected function initializeStandaloneViewInstance() {
106 $this->view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
107 }
108
109 /**
110 * Set template
111 *
112 * @param array $conf With possibly set file resource
113 * @return void
114 */
115 protected function setTemplate(array $conf) {
116 // Fetch the Fluid template
117 $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
118 /** @var $templateService \TYPO3\CMS\Core\TypoScript\TemplateService */
119 $templateService = $GLOBALS['TSFE']->tmpl;
120 $templatePathAndFilename = $templateService->getFileName($file);
121 $this->view->setTemplatePathAndFilename($templatePathAndFilename);
122 }
123
124 /**
125 * Set layout root path if given in configuration
126 *
127 * @param array $conf Configuration array
128 * @return void
129 */
130 protected function setLayoutRootPath(array $conf) {
131 $layoutRootPath = isset($conf['layoutRootPath.']) ? $this->cObj->stdWrap($conf['layoutRootPath'], $conf['layoutRootPath.']) : $conf['layoutRootPath'];
132 if ($layoutRootPath) {
133 $layoutRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($layoutRootPath);
134 $this->view->setLayoutRootPath($layoutRootPath);
135 }
136 }
137
138 /**
139 * Set partial root path if given in configuration
140 *
141 * @param array $conf Configuration array
142 * @return void
143 */
144 protected function setPartialRootPath(array $conf) {
145 $partialRootPath = isset($conf['partialRootPath.']) ? $this->cObj->stdWrap($conf['partialRootPath'], $conf['partialRootPath.']) : $conf['partialRootPath'];
146 if ($partialRootPath) {
147 $partialRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($partialRootPath);
148 $this->view->setPartialRootPath($partialRootPath);
149 }
150 }
151
152 /**
153 * Set different format if given in configuration
154 *
155 * @param array $conf Configuration array
156 * @return void
157 */
158 protected function setFormat(array $conf) {
159 $format = isset($conf['format.']) ? $this->cObj->stdWrap($conf['format'], $conf['format.']) : $conf['format'];
160 if ($format) {
161 $this->view->setFormat($format);
162 }
163 }
164
165 /**
166 * Set some extbase variables if given
167 *
168 * @param array $conf Configuration array
169 * @return void
170 */
171 protected function setExtbaseVariables(array $conf) {
172 /** @var $request \TYPO3\CMS\Extbase\Mvc\Request */
173 $requestPluginName = isset($conf['extbase.']['pluginName.']) ? $this->cObj->stdWrap($conf['extbase.']['pluginName'], $conf['extbase.']['pluginName.']) : $conf['extbase.']['pluginName'];
174 if ($requestPluginName) {
175 $this->view->getRequest()->setPluginName($requestPluginName);
176 }
177 $requestControllerExtensionName = isset($conf['extbase.']['controllerExtensionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerExtensionName'], $conf['extbase.']['controllerExtensionName.']) : $conf['extbase.']['controllerExtensionName'];
178 if ($requestControllerExtensionName) {
179 $this->view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
180 }
181 $requestControllerName = isset($conf['extbase.']['controllerName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerName'], $conf['extbase.']['controllerName.']) : $conf['extbase.']['controllerName'];
182 if ($requestControllerName) {
183 $this->view->getRequest()->setControllerName($requestControllerName);
184 }
185 $requestControllerActionName = isset($conf['extbase.']['controllerActionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerActionName'], $conf['extbase.']['controllerActionName.']) : $conf['extbase.']['controllerActionName'];
186 if ($requestControllerActionName) {
187 $this->view->getRequest()->setControllerActionName($requestControllerActionName);
188 }
189 }
190
191 /**
192 * Assign rendered content objects in variables array to view
193 *
194 * @param array $conf Configuration array
195 * @return void
196 * @throws \InvalidArgumentException
197 */
198 protected function assignContentObjectVariables(array $conf) {
199 $reservedVariables = array('data', 'current');
200 // Accumulate the variables to be replaced and loop them through cObjGetSingle
201 $variables = (array)$conf['variables.'];
202 foreach ($variables as $variableName => $cObjType) {
203 if (is_array($cObjType)) {
204 continue;
205 }
206 if (!in_array($variableName, $reservedVariables)) {
207 $this->view->assign(
208 $variableName,
209 $this->cObj->cObjGetSingle($cObjType, $variables[$variableName . '.'])
210 );
211 } else {
212 throw new \InvalidArgumentException(
213 'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
214 1288095720
215 );
216 }
217 }
218 }
219
220 /**
221 * Assign content object renderer data and current to view
222 *
223 * @param array $conf Configuration
224 * @return void
225 */
226 protected function assignContentObjectDataAndCurrent(array $conf) {
227 $this->view->assign('data', $this->cObj->data);
228 $this->view->assign('current', $this->cObj->data[$this->cObj->currentValKey]);
229 }
230
231 /**
232 * Render fluid standalone view
233 *
234 * @return string
235 */
236 protected function renderFluidView() {
237 return $this->view->render();
238 }
239
240 /**
241 * Apply standard wrap to content
242 *
243 * @param string $content Rendered HTML content
244 * @param array $conf Configuration array
245 * @return string Standard wrapped content
246 */
247 protected function applyStandardWrapToRenderedContent($content, array $conf) {
248 if (isset($conf['stdWrap.'])) {
249 $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
250 }
251 return $content;
252 }
253
254 }
255 ?>