GetIdViewHelper.php 1.5 KB

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