src/Twig/Extension/NavigationExtension.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Twig\Extension;
  15. use App\Website\LinkGenerator\AbstractProductLinkGenerator;
  16. use App\Website\LinkGenerator\CategoryLinkGenerator;
  17. use Pimcore\Model\Document;
  18. use Pimcore\Navigation\Container;
  19. use Pimcore\Navigation\Page\Document as NavDocument;
  20. use Pimcore\Twig\Extension\Templating\Navigation;
  21. use Pimcore\Twig\Extension\Templating\Placeholder;
  22. use Twig\Extension\AbstractExtension;
  23. use Twig\TwigFunction;
  24. class NavigationExtension extends AbstractExtension
  25. {
  26.     const NAVIGATION_EXTENSION_POINT_PROPERTY 'navigation_extension_point';
  27.     /**
  28.      * @var CategoryLinkGenerator
  29.      */
  30.     protected $categoryLinkGenerator;
  31.     /**
  32.      * @var Navigation
  33.      */
  34.     protected $navigationHelper;
  35.     /**
  36.      * @var Placeholder
  37.      */
  38.     protected $placeholderHelper;
  39.     /**
  40.      * @param Navigation $navigationHelper
  41.      */
  42.     public function __construct(Navigation $navigationHelperPlaceholder $placeholderHelperCategoryLinkGenerator $categoryLinkGenerator)
  43.     {
  44.         $this->navigationHelper $navigationHelper;
  45.         $this->categoryLinkGenerator $categoryLinkGenerator;
  46.         $this->placeholderHelper $placeholderHelper;
  47.     }
  48.     /**
  49.      * @return array|TwigFunction[]
  50.      */
  51.     public function getFunctions()
  52.     {
  53.         return [
  54.             new TwigFunction('app_navigation_data_links', [$this'getDataLinks']),
  55.             new TwigFunction('app_navigation_enrich_breadcrumbs', [$this'enrichBreadcrumbs'])
  56.         ];
  57.     }
  58.     /**
  59.      * @param Document $document
  60.      * @param Document $startNode
  61.      *
  62.      * @return \Pimcore\Navigation\Container
  63.      */
  64.     public function getDataLinks(Document $documentDocument $startNode)
  65.     {
  66.         $navigation $this->navigationHelper->build([
  67.             'active' => $document,
  68.             'root' => $startNode,
  69.             'pageCallback' => function($page$document) {
  70.                 if ($document->getProperty(self::NAVIGATION_EXTENSION_POINT_PROPERTY) == 'category' && $rootCategory $document->getProperty(AbstractProductLinkGenerator::ROOT_CATEGORY_PROPERTY_NAME)) {
  71.                     foreach ($rootCategory->getChildren() as $category) {
  72.                         $categoryPage = new NavDocument([
  73.                             'label' => $category->getName(),
  74.                             'id' => 'category-' $category->getId(),
  75.                             'uri' => $this->categoryLinkGenerator->generate($category, ['rootCategory' => $rootCategory'page' => null], true)
  76.                         ]);
  77.                         foreach ($category->getChildren() as $subCategory) {
  78.                             $subCategoryPage = new NavDocument([
  79.                                 'label' => $subCategory->getName(),
  80.                                 'id' => 'category-' $subCategory->getId(),
  81.                                 'uri' => $this->categoryLinkGenerator->generate($subCategory, ['rootCategory' => $rootCategory'page' => null], true)
  82.                             ]);
  83.                             $categoryPage->addPage($subCategoryPage);
  84.                         }
  85.                         $page->addPage($categoryPage);
  86.                     }
  87.                 }
  88.             }
  89.         ]);
  90.         return $navigation;
  91.     }
  92.     /**
  93.      * @param Container $navigation
  94.      *
  95.      * @return Container
  96.      *
  97.      * @throws \Exception
  98.      */
  99.     public function enrichBreadcrumbs(Container $navigation): Container
  100.     {
  101.         $additionalBreadCrumbs $this->placeholderHelper->__invoke('addBreadcrumb');
  102.         if ($additionalBreadCrumbs->getArrayCopy()) {
  103.             $parentPage false;
  104.             foreach ($additionalBreadCrumbs->getArrayCopy() as $breadcrumb) {
  105.                 $page $navigation->findBy('id'$breadcrumb['id']);
  106.                 if (null === $page) {
  107.                     $parentPage $parentPage ?: $navigation->findBy('id'$breadcrumb['parentId']);
  108.                     $newPage = new \Pimcore\Navigation\Page\Document([
  109.                         'id' => $breadcrumb['id'],
  110.                         'uri' => isset($breadcrumb['url']) && $breadcrumb['url'] != '' $breadcrumb['url'] : '',
  111.                         'label' => $breadcrumb['label'],
  112.                         'active' => true
  113.                     ]);
  114.                     if ($parentPage) {
  115.                         $parentPage->setActive(false);
  116.                         $parentPage->addPage($newPage);
  117.                         $parentPage $newPage;
  118.                     } else {
  119.                         $navigation->addPage($newPage);
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.         return $navigation;
  125.     }
  126. }