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

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