‪TYPO3CMS  10.4
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 
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
30 
84 class ‪CObjectViewHelper extends AbstractViewHelper
85 {
86  use CompileWithContentArgumentAndRenderStatic;
87 
93  protected ‪$escapeChildren = false;
94 
100  protected ‪$escapeOutput = false;
101 
105  protected static ‪$tsfeBackup;
106 
112  public function ‪initializeArguments()
113  {
114  $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');
115  $this->registerArgument('typoscriptObjectPath', 'string', 'the TypoScript setup path of the TypoScript object to render', true);
116  $this->registerArgument('currentValueKey', 'string', 'currentValueKey');
117  $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, '');
118  }
119 
129  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
130  {
131  $data = $renderChildrenClosure();
132  $typoscriptObjectPath = $arguments['typoscriptObjectPath'];
133  $currentValueKey = $arguments['currentValueKey'];
134  $table = $arguments['table'];
135  $contentObjectRenderer = static::getContentObjectRenderer();
136  if (!isset(‪$GLOBALS['TSFE']) || !(‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController)) {
137  static::simulateFrontendEnvironment();
138  }
139  $currentValue = null;
140  if (is_object($data)) {
142  } elseif (is_string($data) || is_numeric($data)) {
143  $currentValue = (string)$data;
144  $data = [$data];
145  }
146  $contentObjectRenderer->start($data, $table);
147  if ($currentValue !== null) {
148  $contentObjectRenderer->setCurrentVal($currentValue);
149  } elseif ($currentValueKey !== null && isset($data[$currentValueKey])) {
150  $contentObjectRenderer->setCurrentVal($data[$currentValueKey]);
151  }
152  $pathSegments = ‪GeneralUtility::trimExplode('.', $typoscriptObjectPath);
153  $lastSegment = (string)array_pop($pathSegments);
154  $setup = static::getConfigurationManager()->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
155  foreach ($pathSegments as $segment) {
156  if (!array_key_exists($segment . '.', $setup)) {
157  throw new Exception(
158  'TypoScript object path "' . $typoscriptObjectPath . '" does not exist',
159  1253191023
160  );
161  }
162  $setup = $setup[$segment . '.'];
163  }
164  if (!isset($setup[$lastSegment])) {
165  throw new Exception(
166  'No Content Object definition found at TypoScript object path "' . $typoscriptObjectPath . '"',
167  1540246570
168  );
169  }
170  $content = ‪self::renderContentObject($contentObjectRenderer, $setup, $typoscriptObjectPath, $lastSegment);
171  if (!isset(‪$GLOBALS['TSFE']) || !(‪$GLOBALS['TSFE'] instanceof TypoScriptFrontendController)) {
172  static::resetFrontendEnvironment();
173  }
174  return $content;
175  }
176 
186  protected static function ‪renderContentObject(ContentObjectRenderer $contentObjectRenderer, array $setup, string $typoscriptObjectPath, string $lastSegment): string
187  {
188  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
189  if ($timeTracker->LR) {
190  $timeTracker->push('/f:cObject/', '<' . $typoscriptObjectPath);
191  }
192  $timeTracker->incStackPointer();
193  $content = $contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.'] ?? [], $typoscriptObjectPath);
194  $timeTracker->decStackPointer();
195  if ($timeTracker->LR) {
196  $timeTracker->pull($content);
197  }
198  return $content;
199  }
200 
204  protected static function ‪getConfigurationManager()
205  {
206  return GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationManagerInterface::class);
207  }
208 
212  protected static function ‪getContentObjectRenderer()
213  {
214  return GeneralUtility::makeInstance(
215  ContentObjectRenderer::class,
216  ‪$GLOBALS['TSFE'] ?? GeneralUtility::makeInstance(TypoScriptFrontendController::class, GeneralUtility::makeInstance(Context::class))
217  );
218  }
219 
224  protected static function ‪simulateFrontendEnvironment()
225  {
226  static::$tsfeBackup = ‪$GLOBALS['TSFE'] ?? null;
227  ‪$GLOBALS['TSFE'] = new \stdClass();
228  ‪$GLOBALS['TSFE']->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
229  ‪$GLOBALS['TSFE']->cObjectDepthCounter = 100;
230  }
231 
237  protected static function ‪resetFrontendEnvironment()
238  {
239  ‪$GLOBALS['TSFE'] = static::$tsfeBackup;
240  }
241 }
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\getContentObjectRenderer
‪static ContentObjectRenderer getContentObjectRenderer()
Definition: CObjectViewHelper.php:208
‪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:125
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper
Definition: CObjectViewHelper.php:85
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\initializeArguments
‪initializeArguments()
Definition: CObjectViewHelper.php:108
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: CObjectViewHelper.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\simulateFrontendEnvironment
‪static simulateFrontendEnvironment()
Definition: CObjectViewHelper.php:220
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettableProperties
‪static array getGettableProperties(object $object)
Definition: ObjectAccess.php:356
‪TYPO3\CMS\Fluid\ViewHelpers
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$tsfeBackup
‪static TYPO3 CMS Frontend Controller TypoScriptFrontendController $tsfeBackup
Definition: CObjectViewHelper.php:101
‪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:233
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CObjectViewHelper.php:91
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\renderContentObject
‪static string renderContentObject(ContentObjectRenderer $contentObjectRenderer, array $setup, string $typoscriptObjectPath, string $lastSegment)
Definition: CObjectViewHelper.php:182
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\cObjGetSingle
‪string cObjGetSingle($name, $conf, $TSkey='__')
Definition: ContentObjectRenderer.php:673
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\getConfigurationManager
‪static ConfigurationManagerInterface getConfigurationManager()
Definition: CObjectViewHelper.php:200