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

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