GetIdViewHelper.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\EventsPage;
  3. use Closure;
  4. use Opentalent\OtCore\Website\OtPageRepository;
  5. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  6. use Opentalent\OtCore\Website\OtWebsiteRepository;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Extbase\Object\ObjectManager;
  9. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  10. /**
  11. * Retrieves and returns the uid of the events' page of the current site
  12. * The events page is the first page found with the template 'OpenTalent.OtTemplating->events'
  13. * Returns null if no such page is found, or if the page is disabled
  14. *
  15. * Call it in fluid templates with:
  16. *
  17. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  18. *
  19. * {ot:eventsPage.getId()}
  20. *
  21. * @package Opentalent\OtTemplating\ViewHelpers
  22. */
  23. class GetIdViewHelper 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. 'children',
  36. 'integer',
  37. 'If true, look for the events page of the children structures, instead of the current one',
  38. false,
  39. 0
  40. );
  41. }
  42. /**
  43. * -- This method is expected by Fluid --
  44. * Renders the content as html
  45. *
  46. * @return int|null
  47. */
  48. public function render(): ?int
  49. {
  50. $rootId = \Opentalent\OtTemplating\ViewHelpers\RootPage\GetIdViewHelper::renderStatic(
  51. $this->arguments,
  52. $this->renderChildrenClosure,
  53. $this->renderingContext
  54. );
  55. $subpages = $this->otPageRepository->getAllSubpagesForPage($rootId);
  56. $templateName = $this->arguments['children'] == 1 ?
  57. 'OpenTalent.OtTemplating->structuresEvents' :
  58. 'OpenTalent.OtTemplating->events';
  59. foreach ($subpages as $page) {
  60. if ($page['tx_fed_page_controller_action'] === $templateName
  61. & $page['deleted'] == 0
  62. & $page['hidden'] == 0
  63. ) {
  64. return $page['uid'];
  65. }
  66. }
  67. return null;
  68. }
  69. }