vendor/shopware/core/Framework/Adapter/Twig/Extension/BuildBreadcrumbExtension.php line 102

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Twig\Extension;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Category\Service\CategoryBreadcrumbBuilder;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Feature;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Twig\Extension\AbstractExtension;
  11. use Twig\TwigFunction;
  12. /**
  13.  * @package core
  14.  */
  15. class BuildBreadcrumbExtension extends AbstractExtension
  16. {
  17.     /**
  18.      * @var CategoryBreadcrumbBuilder
  19.      */
  20.     private $categoryBreadcrumbBuilder;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $categoryRepository;
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(CategoryBreadcrumbBuilder $categoryBreadcrumbBuilderEntityRepositoryInterface $categoryRepository)
  29.     {
  30.         $this->categoryBreadcrumbBuilder $categoryBreadcrumbBuilder;
  31.         $this->categoryRepository $categoryRepository;
  32.     }
  33.     public function getFunctions(): array
  34.     {
  35.         return [
  36.             new TwigFunction('sw_breadcrumb_full', [$this'getFullBreadcrumb'], ['needs_context' => true]),
  37.             /*
  38.              * @deprecated tag:v6.5.0 - Will be deleted, use sw_breadcrumb_full instead.
  39.              */
  40.             new TwigFunction('sw_breadcrumb', [$this'buildSeoBreadcrumb'], ['needs_context' => true]),
  41.             /*
  42.              * @deprecated tag:v6.5.0 - Will be deleted, use sw_breadcrumb_full instead.
  43.              */
  44.             new TwigFunction('sw_breadcrumb_types', [$this'getCategoryTypes']),
  45.             /*
  46.              * @deprecated tag:v6.5.0 - Will be deleted, without a replacement.
  47.              */
  48.             new TwigFunction('sw_breadcrumb_build_types', [$this'buildCategoryTypes']),
  49.         ];
  50.     }
  51.     public function getFullBreadcrumb(array $twigContextCategoryEntity $categoryContext $context): array
  52.     {
  53.         $salesChannel null;
  54.         if (\array_key_exists('context'$twigContext) && $twigContext['context'] instanceof SalesChannelContext) {
  55.             $salesChannel $twigContext['context']->getSalesChannel();
  56.         }
  57.         $seoBreadcrumb $this->categoryBreadcrumbBuilder->build($category$salesChannel);
  58.         if ($seoBreadcrumb === null) {
  59.             return [];
  60.         }
  61.         $categoryIds array_keys($seoBreadcrumb);
  62.         if (empty($categoryIds)) {
  63.             return [];
  64.         }
  65.         $criteria = new Criteria($categoryIds);
  66.         $criteria->setTitle('breadcrumb-extension');
  67.         $categories $this->categoryRepository->search($criteria$context)->getEntities();
  68.         $breadcrumb = [];
  69.         foreach ($categoryIds as $categoryId) {
  70.             if ($categories->get($categoryId) === null) {
  71.                 continue;
  72.             }
  73.             $breadcrumb[$categoryId] = $categories->get($categoryId);
  74.         }
  75.         return $breadcrumb;
  76.     }
  77.     /**
  78.      * @deprecated tag:v6.5.0 - Will be set to private or deleted, without a replacement.
  79.      */
  80.     public function buildSeoBreadcrumb(array $twigContextCategoryEntity $category, ?string $navigationCategoryId null): ?array
  81.     {
  82.         Feature::triggerDeprecationOrThrow(
  83.             'v6.5.0.0',
  84.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''sw_breadcrumb_full')
  85.         );
  86.         $salesChannel null;
  87.         if (\array_key_exists('context'$twigContext) && $twigContext['context'] instanceof SalesChannelContext) {
  88.             $salesChannel $twigContext['context']->getSalesChannel();
  89.         }
  90.         return $this->categoryBreadcrumbBuilder->build($category$salesChannel$navigationCategoryId);
  91.     }
  92.     /**
  93.      * @deprecated tag:v6.5.0 - Will be deleted, use getFullBreadcrumb instead.
  94.      *
  95.      * @param array<string> $categoryIds
  96.      */
  97.     public function getCategoryTypes(array $categoryIdsContext $context): array
  98.     {
  99.         Feature::triggerDeprecationOrThrow(
  100.             'v6.5.0.0',
  101.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''sw_breadcrumb_full')
  102.         );
  103.         return $this->buildCategoryTypes($this->getCategories($categoryIds$context));
  104.     }
  105.     /**
  106.      * @deprecated tag:v6.5.0 - Will be deleted, use getFullBreadcrumb instead.
  107.      *
  108.      * @param CategoryEntity[] $categories
  109.      */
  110.     public function buildCategoryTypes(array $categories): array
  111.     {
  112.         Feature::triggerDeprecationOrThrow(
  113.             'v6.5.0.0',
  114.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''sw_breadcrumb_full')
  115.         );
  116.         if (\count($categories) === 0) {
  117.             return [];
  118.         }
  119.         $types = [];
  120.         foreach ($categories as $category) {
  121.             $types[$category->getId()] = $category->getType();
  122.         }
  123.         return $types;
  124.     }
  125.     /**
  126.      * @param array<string> $categoryIds
  127.      */
  128.     private function getCategories(array $categoryIdsContext $context): array
  129.     {
  130.         if (\count($categoryIds) === 0) {
  131.             return [];
  132.         }
  133.         return $this->categoryRepository->search(new Criteria($categoryIds), $context)->getElements();
  134.     }
  135. }