TYPO3 CMS  TYPO3_8-7
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 
24 
29 {
33  protected $view = null;
34 
39 
44  {
45  parent::__construct($cObj);
46  $this->contentDataProcessor = GeneralUtility::makeInstance(ContentDataProcessor::class);
47  }
48 
53  {
54  $this->contentDataProcessor = $contentDataProcessor;
55  }
56 
85  public function render($conf = [])
86  {
87  $parentView = $this->view;
89 
90  if (!is_array($conf)) {
91  $conf = [];
92  }
93 
94  $this->setFormat($conf);
95  $this->setTemplate($conf);
96  $this->setLayoutRootPath($conf);
97  $this->setPartialRootPath($conf);
98  $this->setExtbaseVariables($conf);
99  $this->assignSettings($conf);
100  $variables = $this->getContentObjectVariables($conf);
101  $variables = $this->contentDataProcessor->process($this->cObj, $conf, $variables);
102 
103  $this->view->assignMultiple($variables);
104 
106  $content = $this->renderFluidView();
107  $content = $this->applyStandardWrapToRenderedContent($content, $conf);
108 
109  $this->view = $parentView;
110  return $content;
111  }
112 
119  {
120  $pageRenderer = $this->getPageRenderer();
121  $headerAssets = $this->view->renderSection('HeaderAssets', ['contentObject' => $this], true);
122  $footerAssets = $this->view->renderSection('FooterAssets', ['contentObject' => $this], true);
123  if (!empty(trim($headerAssets))) {
124  $pageRenderer->addHeaderData($headerAssets);
125  }
126  if (!empty(trim($footerAssets))) {
127  $pageRenderer->addFooterData($footerAssets);
128  }
129  }
130 
138  protected function initializeStandaloneViewInstance()
139  {
140  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
141  }
142 
149  protected function setTemplate(array $conf)
150  {
151  // Fetch the Fluid template by templateName
152  if (
153  (!empty($conf['templateName']) || !empty($conf['templateName.']))
154  && !empty($conf['templateRootPaths.']) && is_array($conf['templateRootPaths.'])
155  ) {
156  $templateRootPaths = $this->applyStandardWrapToFluidPaths($conf['templateRootPaths.']);
157  $this->view->setTemplateRootPaths($templateRootPaths);
158  $templateName = isset($conf['templateName.'])
159  ? $this->cObj->stdWrap(isset($conf['templateName']) ? $conf['templateName'] : '', $conf['templateName.'])
160  : $conf['templateName'];
161  $this->view->setTemplate($templateName);
162  } elseif (!empty($conf['template']) && !empty($conf['template.'])) {
163  // Fetch the Fluid template by template cObject
164  $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.']);
165  $this->view->setTemplateSource($templateSource);
166  } else {
167  // Fetch the Fluid template by file stdWrap
168  $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
170  $templateService = $GLOBALS['TSFE']->tmpl;
171  $templatePathAndFilename = $templateService->getFileName($file);
172  $this->view->setTemplatePathAndFilename(PATH_site . $templatePathAndFilename);
173  }
174  }
175 
181  protected function setLayoutRootPath(array $conf)
182  {
183  // Override the default layout path via typoscript
184  $layoutPaths = [];
185  if (isset($conf['layoutRootPath']) || isset($conf['layoutRootPath.'])) {
186  $layoutRootPath = isset($conf['layoutRootPath.'])
187  ? $this->cObj->stdWrap($conf['layoutRootPath'], $conf['layoutRootPath.'])
188  : $conf['layoutRootPath'];
189  $layoutPaths[] = GeneralUtility::getFileAbsFileName($layoutRootPath);
190  }
191  if (isset($conf['layoutRootPaths.'])) {
192  $layoutPaths = array_replace($layoutPaths, $this->applyStandardWrapToFluidPaths($conf['layoutRootPaths.']));
193  }
194  if (!empty($layoutPaths)) {
195  $this->view->setLayoutRootPaths($layoutPaths);
196  }
197  }
198 
204  protected function setPartialRootPath(array $conf)
205  {
206  $partialPaths = [];
207  if (isset($conf['partialRootPath']) || isset($conf['partialRootPath.'])) {
208  $partialRootPath = isset($conf['partialRootPath.'])
209  ? $this->cObj->stdWrap($conf['partialRootPath'], $conf['partialRootPath.'])
210  : $conf['partialRootPath'];
211  $partialPaths[] = GeneralUtility::getFileAbsFileName($partialRootPath);
212  }
213  if (isset($conf['partialRootPaths.'])) {
214  $partialPaths = array_replace($partialPaths, $this->applyStandardWrapToFluidPaths($conf['partialRootPaths.']));
215  }
216  if (!empty($partialPaths)) {
217  $this->view->setPartialRootPaths($partialPaths);
218  }
219  }
220 
226  protected function setFormat(array $conf)
227  {
228  $format = isset($conf['format.']) ? $this->cObj->stdWrap($conf['format'], $conf['format.']) : $conf['format'];
229  if ($format) {
230  $this->view->setFormat($format);
231  }
232  }
233 
239  protected function setExtbaseVariables(array $conf)
240  {
242  $requestPluginName = isset($conf['extbase.']['pluginName.']) ? $this->cObj->stdWrap($conf['extbase.']['pluginName'], $conf['extbase.']['pluginName.']) : $conf['extbase.']['pluginName'];
243  if ($requestPluginName) {
244  $this->view->getRequest()->setPluginName($requestPluginName);
245  }
246  $requestControllerExtensionName = isset($conf['extbase.']['controllerExtensionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerExtensionName'], $conf['extbase.']['controllerExtensionName.']) : $conf['extbase.']['controllerExtensionName'];
247  if ($requestControllerExtensionName) {
248  $this->view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
249  }
250  $requestControllerName = isset($conf['extbase.']['controllerName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerName'], $conf['extbase.']['controllerName.']) : $conf['extbase.']['controllerName'];
251  if ($requestControllerName) {
252  $this->view->getRequest()->setControllerName($requestControllerName);
253  }
254  $requestControllerActionName = isset($conf['extbase.']['controllerActionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerActionName'], $conf['extbase.']['controllerActionName.']) : $conf['extbase.']['controllerActionName'];
255  if ($requestControllerActionName) {
256  $this->view->getRequest()->setControllerActionName($requestControllerActionName);
257  }
258 
259  if (
260  $requestPluginName
261  && $requestControllerExtensionName
262  && $requestControllerName
263  && $requestControllerActionName
264  ) {
265  $configurationManager = GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationManager::class);
266  $configurationManager->setConfiguration([
267  'extensionName' => $requestControllerExtensionName,
268  'pluginName' => $requestPluginName,
269  ]);
270 
271  if (!isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$requestControllerExtensionName]['plugins'][$requestPluginName]['controllers'])) {
272  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$requestControllerExtensionName]['plugins'][$requestPluginName]['controllers'] = [
273  $requestControllerName => [
274  'actions' => [
275  $requestControllerActionName,
276  ],
277  ],
278  ];
279  }
280 
281  $requestBuilder = GeneralUtility::makeInstance(ObjectManager::class)->get(RequestBuilder::class);
282  $this->view->getRenderingContext()->getControllerContext()->setRequest($requestBuilder->build());
283  }
284  }
285 
293  protected function getContentObjectVariables(array $conf)
294  {
295  $variables = [];
296  $reservedVariables = ['data', 'current'];
297  // Accumulate the variables to be process and loop them through cObjGetSingle
298  $variablesToProcess = (array)$conf['variables.'];
299  foreach ($variablesToProcess as $variableName => $cObjType) {
300  if (is_array($cObjType)) {
301  continue;
302  }
303  if (!in_array($variableName, $reservedVariables)) {
304  $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $variablesToProcess[$variableName . '.']);
305  } else {
306  throw new \InvalidArgumentException(
307  'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
308  1288095720
309  );
310  }
311  }
312  $variables['data'] = $this->cObj->data;
313  $variables['current'] = $this->cObj->data[$this->cObj->currentValKey];
314  return $variables;
315  }
316 
323  protected function assignSettings(array $conf)
324  {
325  if (isset($conf['settings.'])) {
327  $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
328  $settings = $typoScriptService->convertTypoScriptArrayToPlainArray($conf['settings.']);
329  $this->view->assign('settings', $settings);
330  }
331  }
332 
338  protected function renderFluidView()
339  {
340  return $this->view->render();
341  }
342 
350  protected function applyStandardWrapToRenderedContent($content, array $conf)
351  {
352  if (isset($conf['stdWrap.'])) {
353  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
354  }
355  return $content;
356  }
357 
365  protected function applyStandardWrapToFluidPaths(array $paths)
366  {
367  $finalPaths = [];
368  foreach ($paths as $key => $path) {
369  if (StringUtility::endsWith($key, '.')) {
370  if (isset($paths[substr($key, 0, -1)])) {
371  continue;
372  }
373  $path = $this->cObj->stdWrap('', $path);
374  } elseif (isset($paths[$key . '.'])) {
375  $path = $this->cObj->stdWrap($path, $paths[$key . '.']);
376  }
377  $finalPaths[$key] = GeneralUtility::getFileAbsFileName($path);
378  }
379  return $finalPaths;
380  }
381 }
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static endsWith($haystack, $needle)