custom/plugins/WoodRepair/src/Subscriber/SalesChannelsSubscriber.php line 227

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TigerMedia\WoodRepair\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  11. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class SalesChannelsSubscriber implements EventSubscriberInterface {
  16.     private EntityRepositoryInterface $salesChannelsRepository;
  17.     private RequestStack $requestStack;
  18.     private EntityRepository $productRepository;
  19.     private Context $context;
  20.     public function __construct
  21.     (
  22.         EntityRepositoryInterface $salesChannelsRepository,
  23.         RequestStack $requestStack,
  24.         EntityRepository $productRepository,
  25.     ) {
  26.         $this->salesChannelsRepository $salesChannelsRepository;
  27.         $this->requestStack $requestStack;
  28.         $this->productRepository $productRepository;
  29.         $this->context Context::createDefaultContext();
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             GenericPageLoadedEvent::class => 'assignVariables',
  35.         ];
  36.     }
  37.     /**
  38.      * @param  string  $route
  39.      * @return string
  40.      */
  41.     private function getMethodNameFromRoute(string $route): string
  42.     {
  43.         $output 'get';
  44.         $split explode('.'$route);
  45.         foreach ($split as $item) {
  46.             $output .= ucfirst(strtolower($item));
  47.         }
  48.         return $output;
  49.     }
  50.     /**
  51.      * @param  string  $productId
  52.      * @return ProductEntity|null
  53.      */
  54.     private function getProductById(string $productId): ?ProductEntity
  55.     {
  56.         $criteria = new Criteria();
  57.         $criteria
  58.             ->addAssociation('seoUrls')
  59.             ->addFilter(new EqualsFilter('id'$productId));
  60.         return $this->productRepository->search($criteria$this->context)->first();
  61.     }
  62.     /**
  63.      * @param  string  $entityId
  64.      * @param  string  $languageId
  65.      * @param  string  $salesChannelId
  66.      * @param  string  $domain
  67.      * @return string
  68.      */
  69.     public function getFrontendDetailPage(
  70.         string $entityId,
  71.         string $languageId,
  72.         string $salesChannelId,
  73.         string $domain
  74.     ): string {
  75.         $_product $this->getProductById($entityId);
  76.         $productId $_product?->getId();
  77.         /**
  78.          * If we don't have the product in this specific sales channel,
  79.          * just link to homepage.
  80.          */
  81.         if (is_null($productId) || !$_product->getActive()) {
  82.             return $domain;
  83.         }
  84.         $seoUrls $_product->getSeoUrls();
  85.         if (is_null($seoUrls)) {
  86.             return $domain '/details/' $_product->getId();
  87.         }
  88.         $seoUrls $seoUrls->jsonSerialize();
  89.         $seo array_filter($seoUrls, function ($seoUrl) use ($salesChannelId$languageId) {
  90.             return ($seoUrl->getSalesChannelId() == $salesChannelId && $seoUrl->getLanguageId() == $languageId);
  91.         });
  92.         $seo end($seo); // getting the latest url.
  93.         if (!$seo) {
  94.             return $domain;
  95.         }
  96.         return $domain '/' $seo->getSeoPathInfo();
  97.     }
  98.     /**
  99.      * @param  string|null  $domain
  100.      * @return string
  101.      */
  102.     private function getFrontendNavigationPage(?string $domain null): string /** @phpstan-ignore-line */
  103.     {
  104.         return ($domain ?? '/');
  105.     }
  106.     /**
  107.      * @param  string|null  $domain
  108.      * @return string
  109.      */
  110.     public function getFrontendHomePage(?string $domain null): string
  111.     {
  112.         return ($domain ?? '/');
  113.     }
  114.     /**
  115.      * @param array $params
  116.      * @return array
  117.      */
  118.     public function getParams(array $params = []): array /** @phpstan-ignore-line */
  119.     {
  120.         $output = [];
  121.         $route $this->requestStack->getCurrentRequest()->attributes->get('_route');
  122.         switch ($route) {
  123.             case 'frontend.detail.page':
  124.                 $output['entityId'] = $this->requestStack->getCurrentRequest()->get('productId');
  125.                 break;
  126.             case 'frontend.navigation.page':
  127.             case 'frontend.home.page':
  128.                 unset($params['languageId']);
  129.                 unset($params['salesChannelId']);
  130.                 break;
  131.         }
  132.         return array_merge($output$params);
  133.     }
  134.     /**
  135.      * @return array
  136.      */
  137.     private function getSalesChannelsPageLinks(): array /** @phpstan-ignore-line */
  138.     {
  139.         $output = [];
  140.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  141.         $method $this->getMethodNameFromRoute($currentRoute);
  142.         /** @var SalesChannelEntity $salesChannel */
  143.         foreach ($this->getSalesChannels() as $salesChannelId => $salesChannel) {
  144.             $url $salesChannel->getDomains()?->first()?->getUrl();
  145.             if ($url === null || str_contains($url'headless')) {
  146.                 continue;
  147.             }
  148.             $domain $salesChannel->getDomains()->first();
  149.             if (is_null($domain)) {
  150.                 continue;
  151.             }
  152.             $language $salesChannel->getLanguage();
  153.             $languageId $domain->getLanguageId();
  154.             $languageName $language->getName();
  155.             $locale $language->getLocale()->getCode();
  156.             $locale strtolower(explode('-'$locale)[1]);
  157.             $locale str_replace('gb''en'$locale);
  158.             $params $this->getParams([
  159.                 'languageId' => $languageId,
  160.                 'salesChannelId' => $salesChannelId,
  161.                 'domain' => $domain->getUrl(),
  162.             ]);
  163.             if (method_exists($this$method)) {
  164.                 $link call_user_func_array([$this$method], $params);
  165.                 if ($link) {
  166.                     $output[$languageId] = [
  167.                         'link' => $link,
  168.                         'languageName' => $languageName,
  169.                         'locale' => $locale
  170.                     ];
  171.                 }
  172.             }
  173.         }
  174.         return $output;
  175.     }
  176.     /**
  177.      * @return EntitySearchResult
  178.      */
  179.     private function getSalesChannels(): EntitySearchResult
  180.     {
  181.         $criteria = new Criteria();
  182.         $criteria->addAssociations(['domains''language''language.locale']);
  183.         return $this->salesChannelsRepository->search($criteriaContext::createDefaultContext());
  184.     }
  185.     public function assignVariables(PageLoadedEvent $event): void
  186.     {
  187.         $event->getPage()->assign([
  188.             'salesChannels' => $this->getSalesChannelsPageLinks(),
  189.             'contextTest' => Context::createDefaultContext(),
  190.         ]);
  191.     }
  192. }