src/Website/Navigation/BreadcrumbHelperService.php line 87

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\Website\Navigation;
  15. use App\Model\Product\Category;
  16. use App\Website\LinkGenerator\AbstractProductLinkGenerator;
  17. use App\Website\LinkGenerator\CategoryLinkGenerator;
  18. use Pimcore\Model\DataObject\AbstractObject;
  19. use Pimcore\Model\DataObject\News;
  20. use Pimcore\Model\Document;
  21. use Pimcore\Translation\Translator;
  22. use Pimcore\Twig\Extension\Templating\Placeholder;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. class BreadcrumbHelperService
  26. {
  27.     /**
  28.      * @var RequestStack
  29.      */
  30.     protected $requestStack;
  31.     /**
  32.      * @var Placeholder
  33.      */
  34.     protected $placeholderHelper;
  35.     /**
  36.      * @var CategoryLinkGenerator
  37.      */
  38.     protected $categoryLinkGenerator;
  39.     /**
  40.      * @var Translator
  41.      */
  42.     protected $translator;
  43.     /**
  44.      * @var UrlGeneratorInterface
  45.      */
  46.     protected $urlGenerator;
  47.     /**
  48.      * BreadcrumbHelperService constructor.
  49.      *
  50.      * @param RequestStack $requestStack
  51.      * @param Placeholder $placeholderHelper
  52.      * @param CategoryLinkGenerator $categoryLinkGenerator
  53.      * @param Translator $translator
  54.      * @param UrlGeneratorInterface $urlGenerator
  55.      */
  56.     public function __construct(RequestStack $requestStack, Placeholder $placeholderHelper, CategoryLinkGenerator $categoryLinkGenerator, Translator $translator, UrlGeneratorInterface $urlGenerator)
  57.     {
  58.         $this->requestStack = $requestStack;
  59.         $this->placeholderHelper = $placeholderHelper;
  60.         $this->categoryLinkGenerator = $categoryLinkGenerator;
  61.         $this->translator = $translator;
  62.         $this->urlGenerator = $urlGenerator;
  63.     }
  64.     /**
  65.      * @return Document
  66.      */
  67.     protected function getCurrentDocument(): Document
  68.     {
  69.         return $this->requestStack->getCurrentRequest()->attributes->get('contentDocument');
  70.     }
  71.     public function enrichProductDetailPage(AbstractObject $product)
  72.     {
  73.         $document = $this->getCurrentDocument();
  74.         //breadcrumbs
  75.         $category = $product->getMainCategory();
  76.         if ($category) {
  77.             $parentId = $document->getId();
  78.             $parentCategories = $category->getParentCategoryList($document->getProperty(AbstractProductLinkGenerator::ROOT_CATEGORY_PROPERTY_NAME));
  79.             $parentCategories[] = $category;
  80.             
  81.             foreach ($parentCategories as $index => $parentCategory) {
  82.                 $parentId = 'category-' . $parentCategory->getId();
  83.                 $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  84.                     'parentId' => $parentId,
  85.                     'id' => 'category-' . $parentCategory->getId(),
  86.                     'url' => $this->categoryLinkGenerator->generate($parentCategory, [], true),
  87.                     'label' => $parentCategory->getName()
  88.                 ]);
  89.             }
  90.         }
  91.         $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  92.             'parentId' => $category ? 'category-' . $category->getParentId() : '',
  93.             'id' => 'product-' . $product->getId(),
  94.             'url' => '#',
  95.             'label' => $product->getOSName() ?? ''
  96.         ]);
  97.     }
  98.     public function enrichCategoryPage(Category $category)
  99.     {
  100.         $document = $this->getCurrentDocument();
  101.         if ($category) {
  102.             $parentId = $document->getId();
  103.             $parentCategories = $category->getParentCategoryList($document->getProperty(AbstractProductLinkGenerator::ROOT_CATEGORY_PROPERTY_NAME));
  104.             $parentCategories[] = $category;
  105.             foreach ($parentCategories as $index => $parentCategory) {
  106.                 $parentId = 'category-' . $parentCategory->getId();
  107.                 $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  108.                     'parentId' => $parentId,
  109.                     'id' => 'category-' . $parentCategory->getId(),
  110.                     'url' => $this->categoryLinkGenerator->generate($parentCategory, [], true),
  111.                     'label' => $parentCategory->getName()
  112.                 ]);
  113.             }
  114.         }
  115.     }
  116.     public function enrichCartPage()
  117.     {
  118.         $document = $this->getCurrentDocument();
  119.         $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  120.             'parentId' => $document->getId(),
  121.             'id' => 'cart',
  122.             'url' => '#',
  123.             'label' => $this->translator->trans('cart.title')
  124.         ]);
  125.     }
  126.     public function enrichCheckoutPage()
  127.     {
  128.         $document = $this->getCurrentDocument();
  129.         $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  130.             'parentId' => $document->getId(),
  131.             'id' => 'cart',
  132.             'url' => $this->urlGenerator->generate('shop-cart-detail'),
  133.             'label' => $this->translator->trans('cart.title')
  134.         ]);
  135.         $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  136.             'parentId' => 'cart',
  137.             'id' => 'checkout',
  138.             'url' => '#',
  139.             'label' => $this->translator->trans('general.checkout')
  140.         ]);
  141.     }
  142.     /**
  143.      * @param News $news
  144.      */
  145.     public function enrichNewsPage(News $news)
  146.     {
  147.         $document = $this->getCurrentDocument();
  148.         $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  149.             'parentId' => $document->getId(),
  150.             'id' => 'news-' . $news->getId(),
  151.             'url' => '#',
  152.             'label' => $news->getTitle()
  153.         ]);
  154.     }
  155.     public function enrichGenericDynamicPage($label)
  156.     {
  157.         $document = $this->getCurrentDocument();
  158.         $this->placeholderHelper->__invoke('addBreadcrumb')->append([
  159.             'parentId' => $document->getId(),
  160.             'id' => 'search-result',
  161.             'label' => $label
  162.         ]);
  163.     }
  164. }