GetFirstWithTemplateViewHelper.php 1.7 KB

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