GetPageUidViewHelper.php 2.0 KB

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