GetFirstWithTemplateViewHelper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /**
  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 FirstWithTemplateViewHelper 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()
  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() {
  45. $rootId = GetIdViewHelper::renderStatic(
  46. $this->arguments,
  47. $this->renderChildrenClosure,
  48. $this->renderingContext
  49. );
  50. $subpages = $this->pageRepository->getAllSubpagesForPage($rootId);
  51. $templateName = 'OpenTalent.OtTemplating->' . $this->arguments['template'];
  52. foreach ($subpages as $page) {
  53. if ($page['tx_fed_page_controller_action'] === $templateName
  54. & $page['deleted'] === 0
  55. & $page['hidden'] === 0
  56. ) {
  57. return $page['uid'];
  58. }
  59. }
  60. return null;
  61. }
  62. }