GetFirstWithTemplateViewHelper.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\OtTemplating\ViewHelpers\RootPage\GetIdViewHelper;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Extbase\Object\ObjectManager;
  9. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  10. /**
  11. * Returns the uid of the first page with the given template in the current website, or null if none
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * {ot:page.getFirstWithTemplate(template: 'foo')}
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class FirstWithTemplateViewHelper extends OtAbstractViewHelper
  20. {
  21. /**
  22. * -- This method is expected by Fluid --
  23. * Declares the viewhelper's parameters
  24. */
  25. public function initializeArguments()
  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. * @param array $arguments
  39. * @param Closure $renderChildrenClosure
  40. * @param RenderingContextInterface $renderingContext
  41. * @return int|null
  42. */
  43. public static function renderStatic(
  44. array $arguments,
  45. Closure $renderChildrenClosure,
  46. RenderingContextInterface $renderingContext
  47. ) {
  48. $rootId = GetIdViewHelper::renderStatic(
  49. $arguments,
  50. $renderChildrenClosure,
  51. $renderingContext
  52. );
  53. $pageRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtPageRepository::class);
  54. $subpages = $pageRepository->getAllSubpagesForPage($rootId);
  55. $templateName = 'OpenTalent.OtTemplating->' . $arguments['template'];
  56. foreach ($subpages as $page) {
  57. if ($page['tx_fed_page_controller_action'] === $templateName
  58. & $page['deleted'] === 0
  59. & $page['hidden'] === 0
  60. ) {
  61. return $page['uid'];
  62. }
  63. }
  64. return null;
  65. }
  66. }