‪TYPO3CMS  11.5
CObjectViewHelper.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 
18 use Psr\Http\Message\ServerRequestInterface;
28 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
30 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
32 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
33 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
34 
88 class ‪CObjectViewHelper extends AbstractViewHelper
89 {
90  use CompileWithContentArgumentAndRenderStatic;
91 
97  protected ‪$escapeChildren = false;
98 
104  protected ‪$escapeOutput = false;
105 
109  protected static ‪$tsfeBackup;
110 
116  public function ‪initializeArguments()
117  {
118  $this->registerArgument('data', 'mixed', 'the data to be used for rendering the cObject. Can be an object, array or string. If this argument is not set, child nodes will be used');
119  $this->registerArgument('typoscriptObjectPath', 'string', 'the TypoScript setup path of the TypoScript object to render', true);
120  $this->registerArgument('currentValueKey', 'string', 'currentValueKey');
121  $this->registerArgument('table', 'string', 'the table name associated with "data" argument. Typically tt_content or one of your custom tables. This argument should be set if rendering a FILES cObject where file references are used, or if the data argument is a database record.', false, '');
122  }
123 
133  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
134  {
135  $data = $renderChildrenClosure();
136  $typoscriptObjectPath = $arguments['typoscriptObjectPath'];
137  $currentValueKey = $arguments['currentValueKey'];
138  $table = $arguments['table'];
139  $contentObjectRenderer = static::getContentObjectRenderer($renderingContext->getRequest());
140  if (!isset(‪$GLOBALS['TSFE']) || !(‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController)) {
141  static::simulateFrontendEnvironment();
142  }
143  $currentValue = null;
144  if (is_object($data)) {
146  } elseif (is_string($data) || is_numeric($data)) {
147  $currentValue = (string)$data;
148  $data = [$data];
149  }
150  $contentObjectRenderer->start($data, $table);
151  if ($currentValue !== null) {
152  $contentObjectRenderer->setCurrentVal($currentValue);
153  } elseif ($currentValueKey !== null && isset($data[$currentValueKey])) {
154  $contentObjectRenderer->setCurrentVal($data[$currentValueKey]);
155  }
156  $pathSegments = ‪GeneralUtility::trimExplode('.', $typoscriptObjectPath);
157  $lastSegment = (string)array_pop($pathSegments);
158  $setup = static::getConfigurationManager()->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
159  foreach ($pathSegments as $segment) {
160  if (!array_key_exists($segment . '.', $setup)) {
161  throw new Exception(
162  'TypoScript object path "' . $typoscriptObjectPath . '" does not exist',
163  1253191023
164  );
165  }
166  $setup = $setup[$segment . '.'];
167  }
168  if (!isset($setup[$lastSegment])) {
169  throw new Exception(
170  'No Content Object definition found at TypoScript object path "' . $typoscriptObjectPath . '"',
171  1540246570
172  );
173  }
174  $content = ‪self::renderContentObject($contentObjectRenderer, $setup, $typoscriptObjectPath, $lastSegment);
175  if (!isset(‪$GLOBALS['TSFE']) || !(‪$GLOBALS['TSFE'] instanceof TypoScriptFrontendController)) {
176  static::resetFrontendEnvironment();
177  }
178  return $content;
179  }
180 
190  protected static function ‪renderContentObject(ContentObjectRenderer $contentObjectRenderer, array $setup, string $typoscriptObjectPath, string $lastSegment): string
191  {
192  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
193  if ($timeTracker->LR) {
194  $timeTracker->push('/f:cObject/', '<' . $typoscriptObjectPath);
195  }
196  $timeTracker->incStackPointer();
197  $content = $contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.'] ?? [], $typoscriptObjectPath);
198  $timeTracker->decStackPointer();
199  if ($timeTracker->LR) {
200  $timeTracker->pull($content);
201  }
202  return $content;
203  }
204 
205  protected static function ‪getConfigurationManager(): ConfigurationManagerInterface
206  {
207  // @todo: this should be replaced by DI once Fluid can handle DI properly
208  return GeneralUtility::getContainer()->get(ConfigurationManagerInterface::class);
209  }
210 
215  protected static function ‪getContentObjectRenderer(ServerRequestInterface $request): ContentObjectRenderer
216  {
217  if ((‪$GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) {
218  $tsfe = ‪$GLOBALS['TSFE'];
219  } else {
220  $site = $request->getAttribute('site');
221  if (!($site instanceof SiteInterface)) {
222  $sites = GeneralUtility::makeInstance(SiteFinder::class)->getAllSites();
223  $site = reset($sites);
224  }
225  $language = $request->getAttribute('language') ?? $site->getDefaultLanguage();
226  $pageArguments = $request->getAttribute('routing') ?? new PageArguments(0, '0', []);
227  $tsfe = GeneralUtility::makeInstance(
228  TypoScriptFrontendController::class,
229  GeneralUtility::makeInstance(Context::class),
230  $site,
231  $language,
232  $pageArguments,
233  GeneralUtility::makeInstance(FrontendUserAuthentication::class)
234  );
235  }
236  return GeneralUtility::makeInstance(ContentObjectRenderer::class, $tsfe);
237  }
238 
242  protected static function ‪simulateFrontendEnvironment()
243  {
244  static::$tsfeBackup = ‪$GLOBALS['TSFE'] ?? null;
245  ‪$GLOBALS['TSFE'] = new \stdClass();
246  ‪$GLOBALS['TSFE']->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
247  }
248 
254  protected static function ‪resetFrontendEnvironment()
255  {
256  ‪$GLOBALS['TSFE'] = static::$tsfeBackup;
257  }
258 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\getContentObjectRenderer
‪static ContentObjectRenderer getContentObjectRenderer(ServerRequestInterface $request)
Definition: CObjectViewHelper.php:211
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\getConfigurationManager
‪static getConfigurationManager()
Definition: CObjectViewHelper.php:201
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\renderStatic
‪static mixed renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CObjectViewHelper.php:129
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper
Definition: CObjectViewHelper.php:89
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\initializeArguments
‪initializeArguments()
Definition: CObjectViewHelper.php:112
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: CObjectViewHelper.php:101
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\simulateFrontendEnvironment
‪static simulateFrontendEnvironment()
Definition: CObjectViewHelper.php:238
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettableProperties
‪static array getGettableProperties(object $object)
Definition: ObjectAccess.php:371
‪TYPO3\CMS\Fluid\ViewHelpers
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$tsfeBackup
‪static TYPO3 CMS Frontend Controller TypoScriptFrontendController $tsfeBackup
Definition: CObjectViewHelper.php:105
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FULL_TYPOSCRIPT
‪const CONFIGURATION_TYPE_FULL_TYPOSCRIPT
Definition: ConfigurationManagerInterface.php:31
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\resetFrontendEnvironment
‪static resetFrontendEnvironment()
Definition: CObjectViewHelper.php:250
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CObjectViewHelper.php:95
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\renderContentObject
‪static string renderContentObject(ContentObjectRenderer $contentObjectRenderer, array $setup, string $typoscriptObjectPath, string $lastSegment)
Definition: CObjectViewHelper.php:186
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:31