bundles/App/Twig/Extension/NavigationExtension.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Extension;
  3. use Pimcore\Model\Document;
  4. use Pimcore\Navigation\Container;
  5. use Pimcore\Navigation\Page\Document as NavDocument;
  6. use Pimcore\Twig\Extension\Templating\Navigation;
  7. use Pimcore\Twig\Extension\Templating\Placeholder;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Twig\Extension\AbstractExtension;
  11. use Twig\TwigFunction;
  12. class NavigationExtension extends AbstractExtension
  13. {
  14. /**
  15. * @var Navigation
  16. */
  17. protected $navigationHelper;
  18. /**
  19. * @var Placeholder
  20. */
  21. protected $placeholderHelper;
  22. protected $breadcrumbActive = '';
  23. protected Request $request;
  24. public function __construct(Navigation $navigationHelper, Placeholder $placeholderHelper, RequestStack $requestStack)
  25. {
  26. $this->navigationHelper = $navigationHelper;
  27. $this->placeholderHelper = $placeholderHelper;
  28. if ($requestStack->getCurrentRequest()) {
  29. $this->request = $requestStack->getCurrentRequest();
  30. }
  31. }
  32. /**
  33. * @return array|TwigFunction[]
  34. */
  35. public function getFunctions()
  36. {
  37. return [
  38. new TwigFunction('app_navigation_enrich_breadcrumbs', [$this, 'enrichBreadcrumbs']),
  39. new TwigFunction('app_navigation_main', [$this, 'mainNavigation']),
  40. ];
  41. }
  42. public function mainNavigation(Document $document, Document $startNode): array
  43. {
  44. $navigation = $this->navigationHelper->build([
  45. 'active' => $document,
  46. 'root' => $startNode,
  47. ]);
  48. $navigationIntern = $navigation->getPages();
  49. $login = $this->request->getSession()->has('login') && $this->request->getSession()->get('login');
  50. foreach ($navigation->getPages() as $key => $page) {
  51. $d = Document::getById($page->getId());
  52. if ($d->getProperty('intern') && !$login) {
  53. unset($navigationIntern[$key]);
  54. }
  55. if ('page' !== $d->getType()) {
  56. unset($navigationIntern[$key]);
  57. }
  58. }
  59. return $navigationIntern;
  60. }
  61. /**
  62. * @throws \Exception
  63. */
  64. public function enrichBreadcrumbs(Document $document, Container $navigation): Container
  65. {
  66. $additionalBreadCrumbs = $this->placeholderHelper->__invoke('addBreadcrumb');
  67. if ($additionalBreadCrumbs->getArrayCopy()) {
  68. $parentPage = false;
  69. foreach ($additionalBreadCrumbs->getArrayCopy() as $breadcrumb) {
  70. $page = $navigation->findBy('id', $breadcrumb['id']);
  71. if (null === $page) {
  72. $parentPage = $parentPage ?: $navigation->findBy('id', $breadcrumb['parentId']);
  73. $newPage = new NavDocument([
  74. 'id' => $breadcrumb['id'],
  75. 'uri' => isset($breadcrumb['url']) && '' != $breadcrumb['url'] ? $breadcrumb['url'] : '',
  76. 'label' => $breadcrumb['label'],
  77. 'active' => true,
  78. ]);
  79. if ($parentPage) {
  80. $parentPage->setActive(false);
  81. $parentPage->addPage($newPage);
  82. $parentPage = $newPage;
  83. } else {
  84. $navigation->addPage($newPage);
  85. }
  86. }
  87. }
  88. }
  89. return $navigation;
  90. }
  91. }