GetPageUidViewHelper.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 TYPO3\CMS\Core\Utility\GeneralUtility;
  7. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  8. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  9. /**
  10. * Returns the uid of the page with the given slug
  11. * if it exists in the current website,
  12. * else returns null
  13. *
  14. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  15. *
  16. * {ot:pageExists(slug: 'foo')}
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers
  19. */
  20. class GetPageUidViewHelper extends AbstractViewHelper
  21. {
  22. /**
  23. * -- This method is expected by Fluid --
  24. * Declares the viewhelper's parameters
  25. */
  26. public function initializeArguments()
  27. {
  28. $this->registerArgument(
  29. 'slug',
  30. 'string',
  31. 'The slug of the researched page',
  32. true
  33. );
  34. }
  35. /**
  36. * -- This method is expected by Fluid --
  37. * Renders the content as html
  38. *
  39. * @param array $arguments
  40. * @param Closure $renderChildrenClosure
  41. * @param RenderingContextInterface $renderingContext
  42. * @return int|null
  43. */
  44. public static function renderStatic(
  45. array $arguments,
  46. Closure $renderChildrenClosure,
  47. RenderingContextInterface $renderingContext
  48. ) {
  49. $rootId = GetIdViewHelper::renderStatic(
  50. $arguments,
  51. $renderChildrenClosure,
  52. $renderingContext
  53. );
  54. $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
  55. $subpages = $pageRepository->getAllSubpagesForPage($rootId);
  56. foreach ($subpages as $page) {
  57. if ($page['slug'] === $arguments['slug']) {
  58. return $page['uid'];
  59. }
  60. }
  61. return null;
  62. }
  63. }