GetFirstWithTemplateViewHelper.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Page;
  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. /**
  12. * Returns the uid of the first page with the given template in the current website, or null if none
  13. *
  14. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  15. *
  16. * {ot:page.getFirstWithTemplate(template: 'foo')}
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers
  19. */
  20. class GetFirstWithTemplateViewHelper extends OtAbstractViewHelper
  21. {
  22. public function __construct(
  23. private readonly OtPageRepository $otPageRepository,
  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. 'template',
  33. 'string',
  34. "The template's name",
  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 = GetIdViewHelper::renderStatic(
  47. $this->arguments,
  48. $this->renderChildrenClosure,
  49. $this->renderingContext
  50. );
  51. $subpages = $this->otPageRepository->getAllSubpagesForPage($rootId);
  52. $templateName = 'OpenTalent.OtTemplating->' . $this->arguments['template'];
  53. foreach ($subpages as $page) {
  54. if ($page['tx_fed_page_controller_action'] === $templateName
  55. & $page['deleted'] === 0
  56. & $page['hidden'] === 0
  57. ) {
  58. return $page['uid'];
  59. }
  60. }
  61. return null;
  62. }
  63. }