‪TYPO3CMS  11.5
FluidTemplateContentObject.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 
29 {
33  protected ‪$view;
34 
38  protected ‪$contentDataProcessor;
39 
43  public function ‪__construct(ContentObjectRenderer ‪$cObj)
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  {
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 = $this->cObj->stdWrapValue('templateName', $conf ?? []);
159  $this->view->setTemplate($templateName);
160  } elseif (!empty($conf['template']) && !empty($conf['template.'])) {
161  // Fetch the Fluid template by template cObject
162  $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.'], 'template');
163  if ($templateSource === '') {
165  'Could not find template source for ' . $conf['template'],
166  1437420865
167  );
168  }
169  $this->view->setTemplateSource($templateSource);
170  } else {
171  // Fetch the Fluid template by file stdWrap
172  $file = (string)$this->cObj->stdWrapValue('file', $conf ?? []);
173  // Get the absolute file name
174  $templatePathAndFilename = GeneralUtility::getFileAbsFileName($file);
175  $this->view->setTemplatePathAndFilename($templatePathAndFilename);
176  }
177  }
178 
184  protected function ‪setLayoutRootPath(array $conf)
185  {
186  // Override the default layout path via typoscript
187  $layoutPaths = [];
188 
189  $layoutRootPath = (string)$this->cObj->stdWrapValue('layoutRootPath', $conf ?? []);
190  if ($layoutRootPath !== '') {
191  $layoutPaths[] = GeneralUtility::getFileAbsFileName($layoutRootPath);
192  }
193  if (isset($conf['layoutRootPaths.'])) {
194  $layoutPaths = array_replace($layoutPaths, $this->‪applyStandardWrapToFluidPaths($conf['layoutRootPaths.']));
195  }
196  if (!empty($layoutPaths)) {
197  $this->view->setLayoutRootPaths($layoutPaths);
198  }
199  }
200 
206  protected function ‪setPartialRootPath(array $conf)
207  {
208  $partialPaths = [];
209 
210  $partialRootPath = (string)$this->cObj->stdWrapValue('partialRootPath', $conf ?? []);
211  if ($partialRootPath !== '') {
212  $partialPaths[] = GeneralUtility::getFileAbsFileName($partialRootPath);
213  }
214  if (isset($conf['partialRootPaths.'])) {
215  $partialPaths = array_replace($partialPaths, $this->‪applyStandardWrapToFluidPaths($conf['partialRootPaths.']));
216  }
217  if (!empty($partialPaths)) {
218  $this->view->setPartialRootPaths($partialPaths);
219  }
220  }
221 
227  protected function ‪setFormat(array $conf)
228  {
229  $format = $this->cObj->stdWrapValue('format', $conf ?? []);
230  if ($format) {
231  $this->view->setFormat($format);
232  }
233  }
234 
240  protected function ‪setExtbaseVariables(array $conf)
241  {
242  $requestPluginName = $this->cObj->stdWrapValue('pluginName', $conf['extbase.'] ?? []);
243  if ($requestPluginName) {
244  $this->view->getRequest()->setPluginName($requestPluginName);
245  }
246  $requestControllerExtensionName = $this->cObj->stdWrapValue('controllerExtensionName', $conf['extbase.'] ?? []);
247  if ($requestControllerExtensionName) {
248  $this->view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
249  }
250  $requestControllerName = $this->cObj->stdWrapValue('controllerName', $conf['extbase.'] ?? []);
251  if ($requestControllerName) {
252  $this->view->getRequest()->setControllerName($requestControllerName);
253  }
254  $requestControllerActionName = $this->cObj->stdWrapValue('controllerActionName', $conf['extbase.'] ?? []);
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(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(RequestBuilder::class);
282  $this->view->getRenderingContext()->setRequest($requestBuilder->build($this->request));
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  $cObjConf = $variablesToProcess[$variableName . '.'] ?? [];
305  $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $cObjConf, 'variables.' . $variableName);
306  } else {
307  throw new \InvalidArgumentException(
308  'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
309  1288095720
310  );
311  }
312  }
313  $variables['data'] = $this->cObj->data;
314  $variables['current'] = $this->cObj->data[$this->cObj->currentValKey ?? null] ?? null;
315  return $variables;
316  }
317 
324  protected function ‪assignSettings(array $conf)
325  {
326  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 (str_ends_with((string)$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 }
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\applyStandardWrapToFluidPaths
‪array applyStandardWrapToFluidPaths(array $paths)
Definition: FluidTemplateContentObject.php:363
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\render
‪string render($conf=[])
Definition: FluidTemplateContentObject.php:83
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:26
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\__construct
‪__construct(ContentObjectRenderer $cObj)
Definition: FluidTemplateContentObject.php:41
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$pageRenderer
‪PageRenderer $pageRenderer
Definition: AbstractContentObject.php:36
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:16
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setContentDataProcessor
‪setContentDataProcessor($contentDataProcessor)
Definition: FluidTemplateContentObject.php:50
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\renderFluidView
‪string renderFluidView()
Definition: FluidTemplateContentObject.php:336
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setFormat
‪setFormat(array $conf)
Definition: FluidTemplateContentObject.php:225
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setPartialRootPath
‪setPartialRootPath(array $conf)
Definition: FluidTemplateContentObject.php:204
‪TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
Definition: ContentRenderingException.php:24
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setTemplate
‪setTemplate(array $conf)
Definition: FluidTemplateContentObject.php:147
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\applyStandardWrapToRenderedContent
‪string applyStandardWrapToRenderedContent($content, array $conf)
Definition: FluidTemplateContentObject.php:348
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\getContentObjectVariables
‪array getContentObjectVariables(array $conf)
Definition: FluidTemplateContentObject.php:291
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\initializeStandaloneViewInstance
‪initializeStandaloneViewInstance()
Definition: FluidTemplateContentObject.php:136
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\$view
‪StandaloneView $view
Definition: FluidTemplateContentObject.php:32
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:29
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$cObj
‪ContentObjectRenderer $cObj
Definition: AbstractContentObject.php:32
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\$contentDataProcessor
‪ContentDataProcessor $contentDataProcessor
Definition: FluidTemplateContentObject.php:36
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:25
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject
Definition: FluidTemplateContentObject.php:29
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\assignSettings
‪assignSettings(array $conf)
Definition: FluidTemplateContentObject.php:322
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\renderFluidTemplateAssetsIntoPageRenderer
‪renderFluidTemplateAssetsIntoPageRenderer()
Definition: FluidTemplateContentObject.php:116
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\getPageRenderer
‪PageRenderer getPageRenderer()
Definition: AbstractContentObject.php:116
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setLayoutRootPath
‪setLayoutRootPath(array $conf)
Definition: FluidTemplateContentObject.php:182
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setExtbaseVariables
‪setExtbaseVariables(array $conf)
Definition: FluidTemplateContentObject.php:238
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
Definition: RequestBuilder.php:37