OtPageRepository.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Opentalent\OtTemplating\Utilities;
  3. use FluidTYPO3\Vhs\Service\PageService;
  4. use TYPO3\CMS\Core\Utility\GeneralUtility;
  5. use TYPO3\CMS\Extbase\Object\ObjectManager;
  6. use TYPO3\CMS\Frontend\Page\PageRepository;
  7. /**
  8. * Class OtPageRepository
  9. *
  10. * Provides some useful methods to query typo3 pages
  11. *
  12. * @package Opentalent\OtTemplating\Page
  13. */
  14. class OtPageRepository extends PageRepository
  15. {
  16. /**
  17. * Returns the root page of the given page,
  18. * or the page itself if the given page is
  19. * already the rootpage of the site
  20. *
  21. * @param $pageUid
  22. *
  23. * @return array
  24. */
  25. public function getRootPageFor($pageUid) {
  26. $pageService = GeneralUtility::makeInstance(ObjectManager::class)->get(PageService::class);
  27. $rootLine = $pageService->getRootLine($pageUid);
  28. for (end($rootLine); key($rootLine)!==null; prev($rootLine)){
  29. $page = current($rootLine);
  30. if ($page['is_siteroot'] == 1) {
  31. return $page;
  32. }
  33. }
  34. return [];
  35. }
  36. /**
  37. * Recursively returns all the subpages of the given page
  38. *
  39. * @param $pageUid
  40. * @return array
  41. */
  42. public function getAllSubpagesForPage($pageUid) {
  43. $subpages = [];
  44. $stack = $this->getSubpagesForPages(
  45. [$pageUid],
  46. '*',
  47. 'sorting',
  48. '',
  49. false
  50. );
  51. foreach ($stack as $page) {
  52. $subpages[] = $page;
  53. $children = $this->getAllSubpagesForPage($page['uid']);
  54. if (!empty($children)) {
  55. $subpages = array_merge($subpages, $children);
  56. }
  57. }
  58. return $subpages;
  59. }
  60. }