‪TYPO3CMS  ‪main
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 
25 
30 {
31  public function ‪__construct(
32  protected ‪ContentDataProcessor $contentDataProcessor,
33  protected ‪StandaloneView $view
34  ) {}
35 
64  public function ‪render($conf = [])
65  {
66  $this->view->setRequest($this->request);
67 
68  if (!is_array($conf)) {
69  $conf = [];
70  }
71 
72  $this->‪setFormat($conf);
73  $this->‪setTemplate($conf);
74  $this->‪setLayoutRootPath($conf);
75  $this->‪setPartialRootPath($conf);
76  $this->‪setExtbaseVariables($conf);
77  $this->‪assignSettings($conf);
78  $variables = $this->‪getContentObjectVariables($conf);
79  $variables = $this->contentDataProcessor->process($this->cObj, $conf, $variables);
80 
81  $this->view->assignMultiple($variables);
82 
84  $content = $this->‪renderFluidView();
85  return $this->‪applyStandardWrapToRenderedContent($content, $conf);
86  }
87 
93  protected function ‪renderFluidTemplateAssetsIntoPageRenderer(array $variables)
94  {
96  $headerAssets = $this->view->renderSection('HeaderAssets', [...$variables, 'contentObject' => $this], true);
97  $footerAssets = $this->view->renderSection('FooterAssets', [...$variables, 'contentObject' => $this], true);
98  if (!empty(trim($headerAssets))) {
99  ‪$pageRenderer->‪addHeaderData($headerAssets);
100  }
101  if (!empty(trim($footerAssets))) {
102  ‪$pageRenderer->‪addFooterData($footerAssets);
103  }
104  }
105 
112  protected function ‪setTemplate(array $conf)
113  {
114  // Fetch the Fluid template by templateName
115  if (
116  (!empty($conf['templateName']) || !empty($conf['templateName.']))
117  && !empty($conf['templateRootPaths.']) && is_array($conf['templateRootPaths.'])
118  ) {
119  $templateRootPaths = $this->‪applyStandardWrapToFluidPaths($conf['templateRootPaths.']);
120  $this->view->setTemplateRootPaths($templateRootPaths);
121  $templateName = $this->cObj->stdWrapValue('templateName', $conf);
122  $this->view->setTemplate($templateName);
123  } elseif (!empty($conf['template']) && !empty($conf['template.'])) {
124  // Fetch the Fluid template by template cObject
125  $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.'], 'template');
126  if ($templateSource === '') {
128  'Could not find template source for ' . $conf['template'],
129  1437420865
130  );
131  }
132  $this->view->setTemplateSource($templateSource);
133  } else {
134  // Fetch the Fluid template by file stdWrap
135  $file = (string)$this->cObj->stdWrapValue('file', $conf);
136  // Get the absolute file name
137  $templatePathAndFilename = GeneralUtility::getFileAbsFileName($file);
138  $this->view->setTemplatePathAndFilename($templatePathAndFilename);
139  }
140  }
141 
147  protected function ‪setLayoutRootPath(array $conf)
148  {
149  // Override the default layout path via typoscript
150  $layoutPaths = [];
151 
152  $layoutRootPath = (string)$this->cObj->stdWrapValue('layoutRootPath', $conf);
153  if ($layoutRootPath !== '') {
154  $layoutPaths[] = GeneralUtility::getFileAbsFileName($layoutRootPath);
155  }
156  if (isset($conf['layoutRootPaths.'])) {
157  $layoutPaths = array_replace($layoutPaths, $this->‪applyStandardWrapToFluidPaths($conf['layoutRootPaths.']));
158  }
159  if (!empty($layoutPaths)) {
160  $this->view->setLayoutRootPaths($layoutPaths);
161  }
162  }
163 
169  protected function ‪setPartialRootPath(array $conf)
170  {
171  $partialPaths = [];
172 
173  $partialRootPath = (string)$this->cObj->stdWrapValue('partialRootPath', $conf);
174  if ($partialRootPath !== '') {
175  $partialPaths[] = GeneralUtility::getFileAbsFileName($partialRootPath);
176  }
177  if (isset($conf['partialRootPaths.'])) {
178  $partialPaths = array_replace($partialPaths, $this->‪applyStandardWrapToFluidPaths($conf['partialRootPaths.']));
179  }
180  if (!empty($partialPaths)) {
181  $this->view->setPartialRootPaths($partialPaths);
182  }
183  }
184 
190  protected function ‪setFormat(array $conf)
191  {
192  $format = $this->cObj->stdWrapValue('format', $conf);
193  if ($format) {
194  $this->view->setFormat($format);
195  }
196  }
197 
203  protected function ‪setExtbaseVariables(array $conf)
204  {
205  // @todo: It is currently unclear if the if's below can happen at all: An extbase request has been
206  // prepared, but the setup of plugin name, controller extension name and friends
207  // did not happen? Maybe these four if's are useless and the main if that
208  // tests for all four properties is fine? Maybe the main if below is obsolete, too?
209  // This comment was added when StandaloneView still had a default constructor that actively
210  // creates a request by default. It might be more possible to resolve this when this is gone.
212  $requestPluginName = (string)$this->cObj->stdWrapValue('pluginName', $conf['extbase.'] ?? []);
213  if ($requestPluginName && ‪$request instanceof ‪RequestInterface) {
214  ‪$request = ‪$request->withPluginName($requestPluginName);
215  $this->view->setRequest(‪$request);
216  }
217  $requestControllerExtensionName = (string)$this->cObj->stdWrapValue('controllerExtensionName', $conf['extbase.'] ?? []);
218  if ($requestControllerExtensionName && ‪$request instanceof ‪RequestInterface) {
219  ‪$request = ‪$request->withControllerExtensionName($requestControllerExtensionName);
220  $this->view->setRequest(‪$request);
221  }
222  $requestControllerName = (string)$this->cObj->stdWrapValue('controllerName', $conf['extbase.'] ?? []);
223  if ($requestControllerName && ‪$request instanceof ‪RequestInterface) {
224  ‪$request = ‪$request->withControllerName($requestControllerName);
225  $this->view->setRequest(‪$request);
226  }
227  $requestControllerActionName = (string)$this->cObj->stdWrapValue('controllerActionName', $conf['extbase.'] ?? []);
228  if ($requestControllerActionName && ‪$request instanceof ‪RequestInterface) {
229  ‪$request = ‪$request->withControllerActionName($requestControllerActionName);
230  $this->view->setRequest(‪$request);
231  }
232 
233  if ($requestPluginName && $requestControllerExtensionName && $requestControllerName && $requestControllerActionName) {
234  // @todo: Yep, ugly. Having all four properties indicates an extbase plugin and then starts
235  // extbase configuration manager. See https://forge.typo3.org/issues/78842 and investigate
236  // if we still need this?
237  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
238  $configurationManager->setConfiguration([
239  'extensionName' => $requestControllerExtensionName,
240  'pluginName' => $requestPluginName,
241  ]);
242  if (!isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$requestControllerExtensionName]['plugins'][$requestPluginName]['controllers'])) {
243  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$requestControllerExtensionName]['plugins'][$requestPluginName]['controllers'] = [
244  $requestControllerName => [
245  'actions' => [
246  $requestControllerActionName,
247  ],
248  ],
249  ];
250  }
251  $requestBuilder = GeneralUtility::makeInstance(RequestBuilder::class);
252  $this->request = $requestBuilder->build($this->request);
253  $this->view->setRequest($this->request);
254  }
255  }
256 
264  protected function ‪getContentObjectVariables(array $conf)
265  {
266  $variables = [];
267  $reservedVariables = ['data', 'current'];
268  // Accumulate the variables to be process and loop them through cObjGetSingle
269  $variablesToProcess = (array)($conf['variables.'] ?? []);
270  foreach ($variablesToProcess as $variableName => $cObjType) {
271  if (is_array($cObjType)) {
272  continue;
273  }
274  if (!in_array($variableName, $reservedVariables)) {
275  $cObjConf = $variablesToProcess[$variableName . '.'] ?? [];
276  $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $cObjConf, 'variables.' . $variableName);
277  } else {
278  throw new \InvalidArgumentException(
279  'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
280  1288095720
281  );
282  }
283  }
284  $variables['data'] = $this->cObj->data;
285  $variables['current'] = $this->cObj->data[$this->cObj->currentValKey] ?? null;
286  return $variables;
287  }
288 
295  protected function ‪assignSettings(array $conf)
296  {
297  if (isset($conf['settings.'])) {
298  $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
299  $settings = $typoScriptService->convertTypoScriptArrayToPlainArray($conf['settings.']);
300  $this->view->assign('settings', $settings);
301  }
302  }
303 
309  protected function ‪renderFluidView()
310  {
311  return $this->view->render();
312  }
313 
321  protected function ‪applyStandardWrapToRenderedContent($content, array $conf)
322  {
323  if (isset($conf['stdWrap.'])) {
324  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
325  }
326  return $content;
327  }
328 
335  protected function ‪applyStandardWrapToFluidPaths(array $paths)
336  {
337  $finalPaths = [];
338  foreach ($paths as $key => $path) {
339  if (str_ends_with((string)$key, '.')) {
340  if (isset($paths[substr($key, 0, -1)])) {
341  continue;
342  }
343  $path = $this->cObj->stdWrap('', $path);
344  } elseif (isset($paths[$key . '.'])) {
345  $path = $this->cObj->stdWrap($path, $paths[$key . '.']);
346  }
347  $finalPaths[$key] = GeneralUtility::getFileAbsFileName($path);
348  }
349  return $finalPaths;
350  }
351 }
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\renderFluidTemplateAssetsIntoPageRenderer
‪renderFluidTemplateAssetsIntoPageRenderer(array $variables)
Definition: FluidTemplateContentObject.php:93
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\applyStandardWrapToFluidPaths
‪array applyStandardWrapToFluidPaths(array $paths)
Definition: FluidTemplateContentObject.php:335
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\render
‪string render($conf=[])
Definition: FluidTemplateContentObject.php:64
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:29
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$pageRenderer
‪PageRenderer $pageRenderer
Definition: AbstractContentObject.php:32
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\getPageRenderer
‪getPageRenderer()
Definition: AbstractContentObject.php:93
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:18
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\renderFluidView
‪string renderFluidView()
Definition: FluidTemplateContentObject.php:309
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setFormat
‪setFormat(array $conf)
Definition: FluidTemplateContentObject.php:190
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$request
‪ServerRequestInterface $request
Definition: AbstractContentObject.php:37
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setPartialRootPath
‪setPartialRootPath(array $conf)
Definition: FluidTemplateContentObject.php:169
‪TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
Definition: ContentRenderingException.php:23
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setTemplate
‪setTemplate(array $conf)
Definition: FluidTemplateContentObject.php:112
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\applyStandardWrapToRenderedContent
‪string applyStandardWrapToRenderedContent($content, array $conf)
Definition: FluidTemplateContentObject.php:321
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\getContentObjectVariables
‪array getContentObjectVariables(array $conf)
Definition: FluidTemplateContentObject.php:264
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\__construct
‪__construct(protected ContentDataProcessor $contentDataProcessor, protected StandaloneView $view)
Definition: FluidTemplateContentObject.php:31
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:31
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:27
‪TYPO3\CMS\Core\Page\PageRenderer\addHeaderData
‪addHeaderData($data)
Definition: PageRenderer.php:696
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject
Definition: FluidTemplateContentObject.php:30
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\assignSettings
‪assignSettings(array $conf)
Definition: FluidTemplateContentObject.php:295
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Page\PageRenderer\addFooterData
‪addFooterData($data)
Definition: PageRenderer.php:708
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setLayoutRootPath
‪setLayoutRootPath(array $conf)
Definition: FluidTemplateContentObject.php:147
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setExtbaseVariables
‪setExtbaseVariables(array $conf)
Definition: FluidTemplateContentObject.php:203
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
Definition: RequestBuilder.php:40