GetPageUidViewHelper.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers;
  3. use Closure;
  4. use Opentalent\OtCore\Website\OtPageRepository;
  5. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  6. use Opentalent\OtCore\Website\OtWebsiteRepository;
  7. use Opentalent\OtTemplating\ViewHelpers\RootPage\GetIdViewHelper;
  8. /**
  9. * Returns the uid of the page with the given slug
  10. * if it exists in the current website,
  11. * else returns null
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * {ot:getPageUid(slug: 'foo')}
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetPageUidViewHelper extends OtAbstractViewHelper
  20. {
  21. public function __construct(
  22. private readonly OtPageRepository $otPageRepository,
  23. private readonly OtWebsiteRepository $otWebsiteRepository,
  24. ) {}
  25. /**
  26. * -- This method is expected by Fluid --
  27. * Declares the viewhelper's parameters
  28. */
  29. public function initializeArguments(): void
  30. {
  31. $this->registerArgument(
  32. 'slug',
  33. 'string',
  34. 'The slug of the researched page',
  35. true
  36. );
  37. }
  38. /**
  39. * -- This method is expected by Fluid --
  40. * Renders the content as html
  41. *
  42. * @return int|null
  43. */
  44. public function render(): ?int
  45. {
  46. $rootId = $this->otWebsiteRepository->getCurrentRootpageUidFromFEGlobals();
  47. $subpages = $this->otPageRepository->getAllSubpagesForPage($rootId);
  48. foreach ($subpages as $page) {
  49. if ($page['slug'] === $this->arguments['slug']) {
  50. return $page['uid'];
  51. }
  52. }
  53. return null;
  54. }
  55. }