vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Twig/Navigation/NavigationTwigExtension.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\WebsiteBundle\Twig\Navigation;
  11. use Sulu\Bundle\WebsiteBundle\Navigation\NavigationMapperInterface;
  12. use Sulu\Component\Content\Mapper\ContentMapperInterface;
  13. use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
  14. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFunction;
  17. /**
  18. * Provides the navigation functions.
  19. */
  20. class NavigationTwigExtension extends AbstractExtension implements NavigationTwigExtensionInterface
  21. {
  22. /**
  23. * @var ContentMapperInterface
  24. */
  25. private $contentMapper;
  26. /**
  27. * @var NavigationMapperInterface
  28. */
  29. private $navigationMapper;
  30. /**
  31. * @var RequestAnalyzerInterface
  32. */
  33. private $requestAnalyzer;
  34. public function __construct(
  35. ContentMapperInterface $contentMapper,
  36. NavigationMapperInterface $navigationMapper,
  37. ?RequestAnalyzerInterface $requestAnalyzer = null
  38. ) {
  39. $this->contentMapper = $contentMapper;
  40. $this->navigationMapper = $navigationMapper;
  41. $this->requestAnalyzer = $requestAnalyzer;
  42. }
  43. public function getFunctions()
  44. {
  45. return [
  46. new TwigFunction('sulu_navigation_root_flat', [$this, 'flatRootNavigationFunction']),
  47. new TwigFunction('sulu_navigation_root_tree', [$this, 'treeRootNavigationFunction']),
  48. new TwigFunction('sulu_navigation_flat', [$this, 'flatNavigationFunction']),
  49. new TwigFunction('sulu_navigation_tree', [$this, 'treeNavigationFunction']),
  50. new TwigFunction('sulu_breadcrumb', [$this, 'breadcrumbFunction']),
  51. new TwigFunction('sulu_navigation_is_active', [$this, 'navigationIsActiveFunction']),
  52. ];
  53. }
  54. public function flatRootNavigationFunction($context = null, $depth = 1, $loadExcerpt = false)
  55. {
  56. $segment = $this->requestAnalyzer->getSegment();
  57. return $this->navigationMapper->getRootNavigation(
  58. $this->requestAnalyzer->getWebspace()->getKey(),
  59. $this->requestAnalyzer->getCurrentLocalization()->getLocale(),
  60. $depth,
  61. true,
  62. $context,
  63. $loadExcerpt,
  64. $segment ? $segment->getKey() : null
  65. );
  66. }
  67. public function treeRootNavigationFunction($context = null, $depth = 1, $loadExcerpt = false)
  68. {
  69. $segment = $this->requestAnalyzer->getSegment();
  70. return $this->navigationMapper->getRootNavigation(
  71. $this->requestAnalyzer->getWebspace()->getKey(),
  72. $this->requestAnalyzer->getCurrentLocalization()->getLocale(),
  73. $depth,
  74. false,
  75. $context,
  76. $loadExcerpt,
  77. $segment ? $segment->getKey() : null
  78. );
  79. }
  80. public function flatNavigationFunction($uuid, $context = null, $depth = 1, $loadExcerpt = false, $level = null)
  81. {
  82. $segment = $this->requestAnalyzer->getSegment();
  83. $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
  84. $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  85. if (null !== $level) {
  86. $breadcrumb = $this->contentMapper->loadBreadcrumb(
  87. $uuid,
  88. $locale,
  89. $webspaceKey
  90. );
  91. // return empty array if level does not exists
  92. if (!isset($breadcrumb[$level])) {
  93. return [];
  94. }
  95. $uuid = $breadcrumb[$level]->getUuid();
  96. }
  97. try {
  98. return $this->navigationMapper->getNavigation(
  99. $uuid,
  100. $webspaceKey,
  101. $locale,
  102. $depth,
  103. true,
  104. $context,
  105. $loadExcerpt,
  106. $segment ? $segment->getKey() : null
  107. );
  108. } catch (DocumentNotFoundException $exception) {
  109. return [];
  110. }
  111. }
  112. public function treeNavigationFunction($uuid, $context = null, $depth = 1, $loadExcerpt = false, $level = null)
  113. {
  114. $segment = $this->requestAnalyzer->getSegment();
  115. $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
  116. $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  117. if (null !== $level) {
  118. $breadcrumb = $this->contentMapper->loadBreadcrumb(
  119. $uuid,
  120. $locale,
  121. $webspaceKey
  122. );
  123. // return empty array if level does not exists
  124. if (!isset($breadcrumb[$level])) {
  125. return [];
  126. }
  127. $uuid = $breadcrumb[$level]->getUuid();
  128. }
  129. try {
  130. return $this->navigationMapper->getNavigation(
  131. $uuid,
  132. $webspaceKey,
  133. $locale,
  134. $depth,
  135. false,
  136. $context,
  137. $loadExcerpt,
  138. $segment ? $segment->getKey() : null
  139. );
  140. } catch (DocumentNotFoundException $exception) {
  141. return [];
  142. }
  143. }
  144. public function breadcrumbFunction($uuid)
  145. {
  146. $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
  147. $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  148. try {
  149. return $this->navigationMapper->getBreadcrumb($uuid, $webspaceKey, $locale);
  150. } catch (DocumentNotFoundException $exception) {
  151. return [];
  152. }
  153. }
  154. /**
  155. * @param string $requestPath
  156. * @param string $itemPath
  157. *
  158. * @return bool
  159. */
  160. public function navigationIsActiveFunction($requestPath, $itemPath)
  161. {
  162. if ($requestPath === $itemPath) {
  163. return true;
  164. }
  165. return \preg_match(\sprintf('/%s([\/]|$)/', \preg_quote($itemPath, '/')), $requestPath);
  166. }
  167. }