‪TYPO3CMS  9.5
CObjectViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
26 
80 class ‪CObjectViewHelper extends AbstractViewHelper
81 {
82  use CompileWithContentArgumentAndRenderStatic;
83 
89  protected ‪$escapeChildren = false;
90 
96  protected ‪$escapeOutput = false;
97 
101  protected static ‪$tsfeBackup;
102 
108  public function ‪initializeArguments()
109  {
110  $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');
111  $this->registerArgument('typoscriptObjectPath', 'string', 'the TypoScript setup path of the TypoScript object to render', true);
112  $this->registerArgument('currentValueKey', 'string', 'currentValueKey');
113  $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, '');
114  }
115 
125  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
126  {
127  $data = $renderChildrenClosure();
128  $typoscriptObjectPath = $arguments['typoscriptObjectPath'];
129  $currentValueKey = $arguments['currentValueKey'];
130  $table = $arguments['table'];
131  $contentObjectRenderer = static::getContentObjectRenderer();
132  if (!isset(‪$GLOBALS['TSFE']) || !(‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController)) {
133  static::simulateFrontendEnvironment();
134  }
135  $currentValue = null;
136  if (is_object($data)) {
138  } elseif (is_string($data) || is_numeric($data)) {
139  $currentValue = (string)$data;
140  $data = [$data];
141  }
142  $contentObjectRenderer->start($data, $table);
143  if ($currentValue !== null) {
144  $contentObjectRenderer->setCurrentVal($currentValue);
145  } elseif ($currentValueKey !== null && isset($data[$currentValueKey])) {
146  $contentObjectRenderer->setCurrentVal($data[$currentValueKey]);
147  }
148  $pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
149  $lastSegment = array_pop($pathSegments);
150  $setup = static::getConfigurationManager()->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
151  foreach ($pathSegments as $segment) {
152  if (!array_key_exists($segment . '.', $setup)) {
153  throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception(
154  'TypoScript object path "' . $typoscriptObjectPath . '" does not exist',
155  1253191023
156  );
157  }
158  $setup = $setup[$segment . '.'];
159  }
160  if (!isset($setup[$lastSegment])) {
161  throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception(
162  'No Content Object definition found at TypoScript object path "' . $typoscriptObjectPath . '"',
163  1540246570
164  );
165  }
166  $content = ‪self::renderContentObject($contentObjectRenderer, $setup, $typoscriptObjectPath, $lastSegment);
167  if (!isset(‪$GLOBALS['TSFE']) || !(‪$GLOBALS['TSFE'] instanceof TypoScriptFrontendController)) {
168  static::resetFrontendEnvironment();
169  }
170  return $content;
171  }
172 
182  protected static function ‪renderContentObject(ContentObjectRenderer $contentObjectRenderer, array $setup, string $typoscriptObjectPath, string $lastSegment): string
183  {
184  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
185  if ($timeTracker->LR) {
186  $timeTracker->push('/f:cObject/', '<' . $typoscriptObjectPath);
187  }
188  $timeTracker->incStackPointer();
189  $content = $contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.'] ?? [], $typoscriptObjectPath);
190  $timeTracker->decStackPointer();
191  if ($timeTracker->LR) {
192  $timeTracker->pull($content);
193  }
194  return $content;
195  }
196 
200  protected static function ‪getConfigurationManager()
201  {
202  return GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationManagerInterface::class);
203  }
204 
208  protected static function ‪getContentObjectRenderer()
209  {
210  return GeneralUtility::makeInstance(
211  ContentObjectRenderer::class,
212  ‪$GLOBALS['TSFE'] ?? GeneralUtility::makeInstance(TypoScriptFrontendController::class, null, 0, 0)
213  );
214  }
215 
220  protected static function ‪simulateFrontendEnvironment()
221  {
222  static::$tsfeBackup = ‪$GLOBALS['TSFE'] ?? null;
223  ‪$GLOBALS['TSFE'] = new \stdClass();
224  ‪$GLOBALS['TSFE']->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
225  ‪$GLOBALS['TSFE']->cObjectDepthCounter = 100;
226  }
227 
233  protected static function ‪resetFrontendEnvironment()
234  {
235  ‪$GLOBALS['TSFE'] = static::$tsfeBackup;
236  }
237 }
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\getContentObjectRenderer
‪static ContentObjectRenderer getContentObjectRenderer()
Definition: CObjectViewHelper.php:204
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:22
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\renderStatic
‪static mixed renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CObjectViewHelper.php:121
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper
Definition: CObjectViewHelper.php:81
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettableProperties
‪static array getGettableProperties($object)
Definition: ObjectAccess.php:372
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\initializeArguments
‪initializeArguments()
Definition: CObjectViewHelper.php:104
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: CObjectViewHelper.php:93
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\simulateFrontendEnvironment
‪static simulateFrontendEnvironment()
Definition: CObjectViewHelper.php:216
‪TYPO3\CMS\Fluid\ViewHelpers
Definition: BaseViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$tsfeBackup
‪static TYPO3 CMS Frontend Controller TypoScriptFrontendController $tsfeBackup
Definition: CObjectViewHelper.php:97
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FULL_TYPOSCRIPT
‪const CONFIGURATION_TYPE_FULL_TYPOSCRIPT
Definition: ConfigurationManagerInterface.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\resetFrontendEnvironment
‪static resetFrontendEnvironment()
Definition: CObjectViewHelper.php:229
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CObjectViewHelper.php:87
‪$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:178
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\cObjGetSingle
‪string cObjGetSingle($name, $conf, $TSkey='__')
Definition: ContentObjectRenderer.php:671
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:27
‪TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper\getConfigurationManager
‪static ConfigurationManagerInterface getConfigurationManager()
Definition: CObjectViewHelper.php:196