TYPO3 CMS  TYPO3_7-6
FluidTemplateContentObject.php
Go to the documentation of this file.
1 <?php
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 
21 
26 {
30  protected $view = null;
31 
36 
41  {
42  parent::__construct($cObj);
43  $this->contentDataProcessor = GeneralUtility::makeInstance(ContentDataProcessor::class);
44  }
45 
50  {
51  $this->contentDataProcessor = $contentDataProcessor;
52  }
53 
82  public function render($conf = [])
83  {
84  $parentView = $this->view;
86 
87  if (!is_array($conf)) {
88  $conf = [];
89  }
90 
91  $this->setFormat($conf);
92  $this->setTemplate($conf);
93  $this->setLayoutRootPath($conf);
94  $this->setPartialRootPath($conf);
95  $this->setExtbaseVariables($conf);
96  $this->assignSettings($conf);
97  $variables = $this->getContentObjectVariables($conf);
98  $variables = $this->contentDataProcessor->process($this->cObj, $conf, $variables);
99 
100  $this->view->assignMultiple($variables);
101 
102  $content = $this->renderFluidView();
103  $content = $this->applyStandardWrapToRenderedContent($content, $conf);
104 
105  $this->view = $parentView;
106  return $content;
107  }
108 
118  protected function initializeStandaloneViewInstance()
119  {
120  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
121  }
122 
130  protected function setTemplate(array $conf)
131  {
132  // Fetch the Fluid template by templateName
133  if (
134  (!empty($conf['templateName']) || !empty($conf['templateName.']))
135  && !empty($conf['templateRootPaths.']) && is_array($conf['templateRootPaths.'])
136  ) {
137  $templateRootPaths = $this->applyStandardWrapToFluidPaths($conf['templateRootPaths.']);
138  $this->view->setTemplateRootPaths($templateRootPaths);
139  $templateName = isset($conf['templateName.'])
140  ? $this->cObj->stdWrap(isset($conf['templateName']) ? $conf['templateName'] : '', $conf['templateName.'])
141  : $conf['templateName'];
142  $this->view->setTemplate($templateName);
143  // Fetch the Fluid template by template cObject
144  } elseif (!empty($conf['template']) && !empty($conf['template.'])) {
145  $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.']);
146  $this->view->setTemplateSource($templateSource);
147  // Fetch the Fluid template by file stdWrap
148  } else {
149  $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
151  $templateService = $GLOBALS['TSFE']->tmpl;
152  $templatePathAndFilename = $templateService->getFileName($file);
153  $this->view->setTemplatePathAndFilename(PATH_site . $templatePathAndFilename);
154  }
155  }
156 
163  protected function setLayoutRootPath(array $conf)
164  {
165  // Override the default layout path via typoscript
166  $layoutPaths = [];
167  if (isset($conf['layoutRootPath']) || isset($conf['layoutRootPath.'])) {
168  $layoutRootPath = isset($conf['layoutRootPath.'])
169  ? $this->cObj->stdWrap($conf['layoutRootPath'], $conf['layoutRootPath.'])
170  : $conf['layoutRootPath'];
171  $layoutPaths[] = GeneralUtility::getFileAbsFileName($layoutRootPath);
172  }
173  if (isset($conf['layoutRootPaths.'])) {
174  $layoutPaths = array_replace($layoutPaths, $this->applyStandardWrapToFluidPaths($conf['layoutRootPaths.']));
175  }
176  if (!empty($layoutPaths)) {
177  $this->view->setLayoutRootPaths($layoutPaths);
178  }
179  }
180 
187  protected function setPartialRootPath(array $conf)
188  {
189  $partialPaths = [];
190  if (isset($conf['partialRootPath']) || isset($conf['partialRootPath.'])) {
191  $partialRootPath = isset($conf['partialRootPath.'])
192  ? $this->cObj->stdWrap($conf['partialRootPath'], $conf['partialRootPath.'])
193  : $conf['partialRootPath'];
194  $partialPaths[] = GeneralUtility::getFileAbsFileName($partialRootPath);
195  }
196  if (isset($conf['partialRootPaths.'])) {
197  $partialPaths = array_replace($partialPaths, $this->applyStandardWrapToFluidPaths($conf['partialRootPaths.']));
198  }
199  if (!empty($partialPaths)) {
200  $this->view->setPartialRootPaths($partialPaths);
201  }
202  }
203 
210  protected function setFormat(array $conf)
211  {
212  $format = isset($conf['format.']) ? $this->cObj->stdWrap($conf['format'], $conf['format.']) : $conf['format'];
213  if ($format) {
214  $this->view->setFormat($format);
215  }
216  }
217 
224  protected function setExtbaseVariables(array $conf)
225  {
227  $requestPluginName = isset($conf['extbase.']['pluginName.']) ? $this->cObj->stdWrap($conf['extbase.']['pluginName'], $conf['extbase.']['pluginName.']) : $conf['extbase.']['pluginName'];
228  if ($requestPluginName) {
229  $this->view->getRequest()->setPluginName($requestPluginName);
230  }
231  $requestControllerExtensionName = isset($conf['extbase.']['controllerExtensionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerExtensionName'], $conf['extbase.']['controllerExtensionName.']) : $conf['extbase.']['controllerExtensionName'];
232  if ($requestControllerExtensionName) {
233  $this->view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
234  }
235  $requestControllerName = isset($conf['extbase.']['controllerName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerName'], $conf['extbase.']['controllerName.']) : $conf['extbase.']['controllerName'];
236  if ($requestControllerName) {
237  $this->view->getRequest()->setControllerName($requestControllerName);
238  }
239  $requestControllerActionName = isset($conf['extbase.']['controllerActionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerActionName'], $conf['extbase.']['controllerActionName.']) : $conf['extbase.']['controllerActionName'];
240  if ($requestControllerActionName) {
241  $this->view->getRequest()->setControllerActionName($requestControllerActionName);
242  }
243  }
244 
252  protected function getContentObjectVariables(array $conf)
253  {
254  $variables = [];
255  $reservedVariables = ['data', 'current'];
256  // Accumulate the variables to be process and loop them through cObjGetSingle
257  $variablesToProcess = (array)$conf['variables.'];
258  foreach ($variablesToProcess as $variableName => $cObjType) {
259  if (is_array($cObjType)) {
260  continue;
261  }
262  if (!in_array($variableName, $reservedVariables)) {
263  $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $variablesToProcess[$variableName . '.']);
264  } else {
265  throw new \InvalidArgumentException(
266  'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
267  1288095720
268  );
269  }
270  }
271  $variables['data'] = $this->cObj->data;
272  $variables['current'] = $this->cObj->data[$this->cObj->currentValKey];
273  return $variables;
274  }
275 
283  protected function assignSettings(array $conf)
284  {
285  if (isset($conf['settings.'])) {
287  $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
288  $settings = $typoScriptService->convertTypoScriptArrayToPlainArray($conf['settings.']);
289  $this->view->assign('settings', $settings);
290  }
291  }
292 
298  protected function renderFluidView()
299  {
300  return $this->view->render();
301  }
302 
310  protected function applyStandardWrapToRenderedContent($content, array $conf)
311  {
312  if (isset($conf['stdWrap.'])) {
313  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
314  }
315  return $content;
316  }
317 
325  protected function applyStandardWrapToFluidPaths(array $paths)
326  {
327  $finalPaths = [];
328  foreach ($paths as $key => $path) {
329  if (StringUtility::endsWith($key, '.')) {
330  if (isset($paths[substr($key, 0, -1)])) {
331  continue;
332  }
333  $path = $this->cObj->stdWrap('', $path);
334  } elseif (isset($paths[$key . '.'])) {
335  $path = $this->cObj->stdWrap($path, $paths[$key . '.']);
336  }
337  $finalPaths[$key] = GeneralUtility::getFileAbsFileName($path);
338  }
339  return $finalPaths;
340  }
341 }
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static endsWith($haystack, $needle)