‪TYPO3CMS  10.4
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 
26 
31 {
35  protected ‪$view;
36 
40  protected ‪$contentDataProcessor;
41 
46  {
47  parent::__construct(‪$cObj);
48  $this->contentDataProcessor = GeneralUtility::makeInstance(ContentDataProcessor::class);
49  }
50 
55  {
56  $this->contentDataProcessor = ‪$contentDataProcessor;
57  }
58 
87  public function ‪render($conf = [])
88  {
89  $parentView = ‪$this->view;
91 
92  if (!is_array($conf)) {
93  $conf = [];
94  }
95 
96  $this->‪setFormat($conf);
97  $this->‪setTemplate($conf);
98  $this->‪setLayoutRootPath($conf);
99  $this->‪setPartialRootPath($conf);
100  $this->‪setExtbaseVariables($conf);
101  $this->‪assignSettings($conf);
102  $variables = $this->‪getContentObjectVariables($conf);
103  $variables = $this->contentDataProcessor->process($this->cObj, $conf, $variables);
104 
105  $this->view->assignMultiple($variables);
106 
108  $content = $this->‪renderFluidView();
109  $content = $this->‪applyStandardWrapToRenderedContent($content, $conf);
110 
111  $this->view = $parentView;
112  return $content;
113  }
114 
121  {
123  $headerAssets = $this->view->renderSection('HeaderAssets', ['contentObject' => $this], true);
124  $footerAssets = $this->view->renderSection('FooterAssets', ['contentObject' => $this], true);
125  if (!empty(trim($headerAssets))) {
126  ‪$pageRenderer->‪addHeaderData($headerAssets);
127  }
128  if (!empty(trim($footerAssets))) {
129  ‪$pageRenderer->‪addFooterData($footerAssets);
130  }
131  }
132 
140  protected function ‪initializeStandaloneViewInstance()
141  {
142  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
143  }
144 
151  protected function ‪setTemplate(array $conf)
152  {
153  // Fetch the Fluid template by templateName
154  if (
155  (!empty($conf['templateName']) || !empty($conf['templateName.']))
156  && !empty($conf['templateRootPaths.']) && is_array($conf['templateRootPaths.'])
157  ) {
158  $templateRootPaths = $this->‪applyStandardWrapToFluidPaths($conf['templateRootPaths.']);
159  $this->view->setTemplateRootPaths($templateRootPaths);
160  $templateName = isset($conf['templateName.'])
161  ? $this->cObj->stdWrap($conf['templateName'] ?? '', $conf['templateName.'])
162  : $conf['templateName'];
163  $this->view->setTemplate($templateName);
164  } elseif (!empty($conf['template']) && !empty($conf['template.'])) {
165  // Fetch the Fluid template by template cObject
166  $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.'], 'template');
167  if ($templateSource === '') {
169  'Could not find template source for ' . $conf['template'],
170  1437420865
171  );
172  }
173  $this->view->setTemplateSource($templateSource);
174  } else {
175  // Fetch the Fluid template by file stdWrap
176  $file = isset($conf['file.'])
177  ? $this->cObj->stdWrap($conf['file'] ?? '', $conf['file.'])
178  : ($conf['file'] ?? '');
179  // Get the absolute file name
180  $templatePathAndFilename = GeneralUtility::getFileAbsFileName($file);
181  $this->view->setTemplatePathAndFilename($templatePathAndFilename);
182  }
183  }
184 
190  protected function ‪setLayoutRootPath(array $conf)
191  {
192  // Override the default layout path via typoscript
193  $layoutPaths = [];
194  if (isset($conf['layoutRootPath']) || isset($conf['layoutRootPath.'])) {
195  $layoutRootPath = isset($conf['layoutRootPath.'])
196  ? $this->cObj->stdWrap($conf['layoutRootPath'], $conf['layoutRootPath.'])
197  : $conf['layoutRootPath'];
198  $layoutPaths[] = GeneralUtility::getFileAbsFileName($layoutRootPath);
199  }
200  if (isset($conf['layoutRootPaths.'])) {
201  $layoutPaths = array_replace($layoutPaths, $this->‪applyStandardWrapToFluidPaths($conf['layoutRootPaths.']));
202  }
203  if (!empty($layoutPaths)) {
204  $this->view->setLayoutRootPaths($layoutPaths);
205  }
206  }
207 
213  protected function ‪setPartialRootPath(array $conf)
214  {
215  $partialPaths = [];
216  if (isset($conf['partialRootPath']) || isset($conf['partialRootPath.'])) {
217  $partialRootPath = isset($conf['partialRootPath.'])
218  ? $this->cObj->stdWrap($conf['partialRootPath'], $conf['partialRootPath.'])
219  : $conf['partialRootPath'];
220  $partialPaths[] = GeneralUtility::getFileAbsFileName($partialRootPath);
221  }
222  if (isset($conf['partialRootPaths.'])) {
223  $partialPaths = array_replace($partialPaths, $this->‪applyStandardWrapToFluidPaths($conf['partialRootPaths.']));
224  }
225  if (!empty($partialPaths)) {
226  $this->view->setPartialRootPaths($partialPaths);
227  }
228  }
229 
235  protected function ‪setFormat(array $conf)
236  {
237  $format = isset($conf['format.'])
238  ? $this->cObj->stdWrap($conf['format'] ?? '', $conf['format.'])
239  : ($conf['format'] ?? '');
240  if ($format) {
241  $this->view->setFormat($format);
242  }
243  }
244 
250  protected function ‪setExtbaseVariables(array $conf)
251  {
252  $requestPluginName = isset($conf['extbase.']['pluginName.'])
253  ? $this->cObj->stdWrap($conf['extbase.']['pluginName'] ?? '', $conf['extbase.']['pluginName.'])
254  : ($conf['extbase.']['pluginName'] ?? '');
255  if ($requestPluginName) {
256  $this->view->getRequest()->setPluginName($requestPluginName);
257  }
258  $requestControllerExtensionName = isset($conf['extbase.']['controllerExtensionName.'])
259  ? $this->cObj->stdWrap($conf['extbase.']['controllerExtensionName'] ?? '', $conf['extbase.']['controllerExtensionName.'])
260  : ($conf['extbase.']['controllerExtensionName'] ?? '');
261  if ($requestControllerExtensionName) {
262  $this->view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
263  }
264  $requestControllerName = isset($conf['extbase.']['controllerName.'])
265  ? $this->cObj->stdWrap($conf['extbase.']['controllerName'] ?? '', $conf['extbase.']['controllerName.'])
266  : ($conf['extbase.']['controllerName'] ?? '');
267  if ($requestControllerName) {
268  $this->view->getRequest()->setControllerName($requestControllerName);
269  }
270  $requestControllerActionName = isset($conf['extbase.']['controllerActionName.'])
271  ? $this->cObj->stdWrap($conf['extbase.']['controllerActionName'] ?? '', $conf['extbase.']['controllerActionName.'])
272  : ($conf['extbase.']['controllerActionName'] ?? '');
273  if ($requestControllerActionName) {
274  $this->view->getRequest()->setControllerActionName($requestControllerActionName);
275  }
276 
277  if (
278  $requestPluginName
279  && $requestControllerExtensionName
280  && $requestControllerName
281  && $requestControllerActionName
282  ) {
283  $configurationManager = GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationManager::class);
284  $configurationManager->setConfiguration([
285  'extensionName' => $requestControllerExtensionName,
286  'pluginName' => $requestPluginName,
287  ]);
288 
289  if (!isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$requestControllerExtensionName]['plugins'][$requestPluginName]['controllers'])) {
290  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$requestControllerExtensionName]['plugins'][$requestPluginName]['controllers'] = [
291  $requestControllerName => [
292  'actions' => [
293  $requestControllerActionName,
294  ],
295  ],
296  ];
297  }
298 
299  $requestBuilder = GeneralUtility::makeInstance(ObjectManager::class)->get(RequestBuilder::class);
300  $this->view->getRenderingContext()->getControllerContext()->setRequest($requestBuilder->build());
301  }
302  }
303 
311  protected function ‪getContentObjectVariables(array $conf)
312  {
313  $variables = [];
314  $reservedVariables = ['data', 'current'];
315  // Accumulate the variables to be process and loop them through cObjGetSingle
316  $variablesToProcess = (array)($conf['variables.'] ?? []);
317  foreach ($variablesToProcess as $variableName => $cObjType) {
318  if (is_array($cObjType)) {
319  continue;
320  }
321  if (!in_array($variableName, $reservedVariables)) {
322  $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $variablesToProcess[$variableName . '.'], 'variables.' . $variableName);
323  } else {
324  throw new \InvalidArgumentException(
325  'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
326  1288095720
327  );
328  }
329  }
330  $variables['data'] = $this->cObj->data;
331  $variables['current'] = $this->cObj->data[$this->cObj->currentValKey ?? null] ?? null;
332  return $variables;
333  }
334 
341  protected function ‪assignSettings(array $conf)
342  {
343  if (isset($conf['settings.'])) {
345  $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
346  $settings = $typoScriptService->convertTypoScriptArrayToPlainArray($conf['settings.']);
347  $this->view->assign('settings', $settings);
348  }
349  }
350 
356  protected function ‪renderFluidView()
357  {
358  return $this->view->render();
359  }
360 
368  protected function ‪applyStandardWrapToRenderedContent($content, array $conf)
369  {
370  if (isset($conf['stdWrap.'])) {
371  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
372  }
373  return $content;
374  }
375 
383  protected function ‪applyStandardWrapToFluidPaths(array $paths)
384  {
385  $finalPaths = [];
386  foreach ($paths as $key => $path) {
387  if (‪StringUtility::endsWith($key, '.')) {
388  if (isset($paths[substr($key, 0, -1)])) {
389  continue;
390  }
391  $path = $this->cObj->stdWrap('', $path);
392  } elseif (isset($paths[$key . '.'])) {
393  $path = $this->cObj->stdWrap($path, $paths[$key . '.']);
394  }
395  $finalPaths[$key] = GeneralUtility::getFileAbsFileName($path);
396  }
397  return $finalPaths;
398  }
399 }
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\applyStandardWrapToFluidPaths
‪array applyStandardWrapToFluidPaths(array $paths)
Definition: FluidTemplateContentObject.php:381
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\render
‪string render($conf=[])
Definition: FluidTemplateContentObject.php:85
‪TYPO3\CMS\Core\Utility\StringUtility\endsWith
‪static bool endsWith($haystack, $needle)
Definition: StringUtility.php:61
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:25
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\__construct
‪__construct(ContentObjectRenderer $cObj)
Definition: FluidTemplateContentObject.php:43
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$pageRenderer
‪PageRenderer $pageRenderer
Definition: AbstractContentObject.php:32
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:16
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setContentDataProcessor
‪setContentDataProcessor($contentDataProcessor)
Definition: FluidTemplateContentObject.php:52
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\renderFluidView
‪string renderFluidView()
Definition: FluidTemplateContentObject.php:354
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setFormat
‪setFormat(array $conf)
Definition: FluidTemplateContentObject.php:233
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setPartialRootPath
‪setPartialRootPath(array $conf)
Definition: FluidTemplateContentObject.php:211
‪TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
Definition: ContentRenderingException.php:25
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setTemplate
‪setTemplate(array $conf)
Definition: FluidTemplateContentObject.php:149
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\applyStandardWrapToRenderedContent
‪string applyStandardWrapToRenderedContent($content, array $conf)
Definition: FluidTemplateContentObject.php:366
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\getContentObjectVariables
‪array getContentObjectVariables(array $conf)
Definition: FluidTemplateContentObject.php:309
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\initializeStandaloneViewInstance
‪initializeStandaloneViewInstance()
Definition: FluidTemplateContentObject.php:138
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\$view
‪StandaloneView $view
Definition: FluidTemplateContentObject.php:34
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$cObj
‪ContentObjectRenderer $cObj
Definition: AbstractContentObject.php:28
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\$contentDataProcessor
‪ContentDataProcessor $contentDataProcessor
Definition: FluidTemplateContentObject.php:38
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:25
‪TYPO3\CMS\Core\Page\PageRenderer\addHeaderData
‪addHeaderData($data)
Definition: PageRenderer.php:977
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject
Definition: FluidTemplateContentObject.php:31
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\assignSettings
‪assignSettings(array $conf)
Definition: FluidTemplateContentObject.php:339
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\renderFluidTemplateAssetsIntoPageRenderer
‪renderFluidTemplateAssetsIntoPageRenderer()
Definition: FluidTemplateContentObject.php:118
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Page\PageRenderer\addFooterData
‪addFooterData($data)
Definition: PageRenderer.php:989
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\getPageRenderer
‪PageRenderer getPageRenderer()
Definition: AbstractContentObject.php:65
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setLayoutRootPath
‪setLayoutRootPath(array $conf)
Definition: FluidTemplateContentObject.php:188
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject\setExtbaseVariables
‪setExtbaseVariables(array $conf)
Definition: FluidTemplateContentObject.php:248
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
Definition: RequestBuilder.php:38