GetPageUidViewHelper.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers;
  3. use Closure;
  4. use Opentalent\OtTemplating\Page\OtPageRepository;
  5. use Opentalent\OtTemplating\ViewHelpers\RootPage\GetIdViewHelper;
  6. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  7. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  8. /**
  9. * Returns the uid of the page with the given slug
  10. * if it exists in the current website,
  11. * else returns null
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * {ot:pageExists(slug: 'foo')}
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetPageUidViewHelper extends AbstractViewHelper
  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. 'slug',
  29. 'string',
  30. 'The slug of the researched page',
  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 = new OtPageRepository();
  54. $subpages = $pageRepository->getAllSubpagesForPage($rootId);
  55. foreach ($subpages as $page) {
  56. if ($page['slug'] === $arguments['slug']) {
  57. return $page['uid'];
  58. }
  59. }
  60. return null;
  61. }
  62. }