GetPageUidViewHelper.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. use TYPO3\CMS\Core\Utility\GeneralUtility;
  9. use TYPO3\CMS\Extbase\Object\ObjectManager;
  10. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  11. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  12. /**
  13. * Returns the uid of the page with the given slug
  14. * if it exists in the current website,
  15. * else returns null
  16. *
  17. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  18. *
  19. * {ot:getPageUid(slug: 'foo')}
  20. *
  21. * @package Opentalent\OtTemplating\ViewHelpers
  22. */
  23. class GetPageUidViewHelper extends OtAbstractViewHelper
  24. {
  25. public function __construct(
  26. private readonly OtPageRepository $otPageRepository,
  27. ) {}
  28. /**
  29. * -- This method is expected by Fluid --
  30. * Declares the viewhelper's parameters
  31. */
  32. public function initializeArguments(): void
  33. {
  34. $this->registerArgument(
  35. 'slug',
  36. 'string',
  37. 'The slug of the researched page',
  38. true
  39. );
  40. }
  41. /**
  42. * -- This method is expected by Fluid --
  43. * Renders the content as html
  44. *
  45. * @return int|null
  46. */
  47. public function render(): ?int
  48. {
  49. $rootId = GetIdViewHelper::renderStatic(
  50. $this->arguments,
  51. $this->renderChildrenClosure,
  52. $this->renderingContext
  53. );
  54. $subpages = $this->otPageRepository->getAllSubpagesForPage($rootId);
  55. foreach ($subpages as $page) {
  56. if ($page['slug'] === $this->arguments['slug']) {
  57. return $page['uid'];
  58. }
  59. }
  60. return null;
  61. }
  62. }