GetIdViewHelper.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\RootPage;
  3. use FluidTYPO3\Vhs\Service\PageService;
  4. use TYPO3\CMS\Core\Utility\GeneralUtility;
  5. use TYPO3\CMS\Extbase\Object\ObjectManager;
  6. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  7. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  8. /**
  9. * Viewhelper to retrieve the Id of the current site root page
  10. * Call it in fluid templates with:
  11. *
  12. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  13. *
  14. * {ot:rootPage.getId()}
  15. *
  16. * @package Opentalent\OtTemplating\ViewHelpers
  17. */
  18. class GetIdViewHelper extends AbstractViewHelper
  19. {
  20. public function initializeArguments()
  21. {
  22. }
  23. /**
  24. * @param array $arguments
  25. * @param \Closure $renderChildrenClosure
  26. * @param RenderingContextInterface $renderingContext
  27. * @return int|null
  28. */
  29. public static function renderStatic(
  30. array $arguments,
  31. \Closure $renderChildrenClosure,
  32. RenderingContextInterface $renderingContext
  33. ) {
  34. $pageUid = $GLOBALS['TSFE']->id;
  35. $pageService = GeneralUtility::makeInstance(ObjectManager::class)->get(PageService::class);
  36. $rootLine = $pageService->getRootLine($pageUid);
  37. for (end($rootLine); key($rootLine)!==null; prev($rootLine)){
  38. $page = current($rootLine);
  39. if ($page['is_siteroot'] == 1) {
  40. return $page['uid'];
  41. }
  42. }
  43. return $rootLine[0]['uid'];
  44. }
  45. }