custom/plugins/TigerLanguageSwitcher/src/Subscriber/SalesChannelSubscriber.php line 86

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TigerMedia\General\TigerLanguageSwitcher\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 SalesChannelSubscriber implements EventSubscriberInterface
  16. {
  17.     private EntityRepository $salesChannelsRepository;
  18.     public function __construct
  19.     (
  20.         EntityRepository $salesChannelsRepository,
  21.     )
  22.     {
  23.         $this->salesChannelsRepository $salesChannelsRepository;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             GenericPageLoadedEvent::class => 'assignVariables',
  29.         ];
  30.     }
  31.     /**
  32.      * @return array<mixed>
  33.      */
  34.     private function getSalesChannelsPageLinks(): array
  35.     {
  36.         $output = [];
  37.         /** @var SalesChannelEntity $salesChannel */
  38.         foreach ($this->getSalesChannels() as $salesChannelId => $salesChannel) {
  39.             $url $salesChannel->getDomains()?->first()?->getUrl();
  40.             if ($url === null || str_contains($url'headless')) {
  41.                 continue;
  42.             }
  43.             $domain $salesChannel->getDomains()->first();
  44.             if (is_null($domain)) {
  45.                 continue;
  46.             }
  47.             $language $salesChannel->getLanguage();
  48.             $languageId $domain->getLanguageId();
  49.             $languageName $language->getName();
  50.             $locale $language->getLocale()->getCode();
  51.             $locale strtolower(explode('-'$locale)[1]);
  52.             $output[$languageId] = [
  53.                 'link' => $domain->getUrl(),
  54.                 'languageName' => $languageName,
  55.                 'locale' => str_replace('gb''en'$locale),
  56.                 'countrycode' => $locale
  57.             ];
  58.         }
  59.         return $output;
  60.     }
  61.     /**
  62.      * @return EntitySearchResult
  63.      */
  64.     private function getSalesChannels(): EntitySearchResult
  65.     {
  66.         $criteria = new Criteria();
  67.         $criteria->addAssociations(['domains''language''language.locale']);
  68.         return $this->salesChannelsRepository->search($criteriaContext::createDefaultContext());
  69.     }
  70.     public function assignVariables(PageLoadedEvent $event): void
  71.     {
  72.         $event->getPage()->assign([
  73.             'salesChannels' => $this->getSalesChannelsPageLinks()
  74.         ]);
  75.     }
  76. }