custom/plugins/CioDeepConfigurator/src/Subscriber/TemplateSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioDeepConfigurator\Subscriber;
  4. use Shopware\Core\Content\Category\Service\NavigationLoader;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Shopware\Core\System\Language\LanguageEntity;
  12. use Shopware\Core\System\Locale\LocaleEntity;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Event\StorefrontRenderEvent;
  15. use Shopware\Storefront\Page\LandingPage\LandingPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Shopware\Storefront\Page\PageLoadedEvent;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class TemplateSubscriber implements EventSubscriberInterface
  20. {
  21.     private SystemConfigService $systemConfigService;
  22.     private EntityRepository $languageRepository;
  23.     private EntityRepository $localeRepository;
  24.     private TranslatorInterface $translator;
  25.     public function __construct(
  26.         SystemConfigService      $systemConfigService,
  27.         EntityRepository         $languageRepository,
  28.         EntityRepository         $localeRepository,
  29.         TranslatorInterface      $translator
  30.     ) {
  31.         $this->systemConfigService = $systemConfigService;
  32.         $this->languageRepository = $languageRepository;
  33.         $this->localeRepository = $localeRepository;
  34.         $this->translator = $translator;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             StorefrontRenderEvent::class => 'onStorefrontRender'
  40.         ];
  41.     }
  42.     public function onStorefrontRender(StorefrontRenderEvent $event)
  43.     {
  44.         $deeplinkConfiguratorId         = $this->systemConfigService->get('CioDeepConfigurator.config.deeplinkConfiguratorId');
  45.         $deeplinkConfiguratorParameters = $this->systemConfigService->get('CioDeepConfigurator.config.deeplinkConfiguratorParameters');
  46.         $locale                    = null;
  47.         $deeplinkConfiguratorLang  = 'de_De';
  48.         $deeplinkConfiguratorShort = 'de';
  49.         $languageId                = $event->getSalesChannelContext()->getLanguageId();
  50.         $language = $this->languageRepository->search(
  51.             (new Criteria())->addFilter(new EqualsFilter('id', $languageId)),
  52.             Context::createDefaultContext()
  53.         )->first();
  54.         if ($language instanceof LanguageEntity) {
  55.             $localeId = $language->getLocaleId();
  56.             $locale = $this->localeRepository->search(
  57.                 (new Criteria())->addFilter(new EqualsFilter('id', $localeId)),
  58.                 Context::createDefaultContext()
  59.             )->first();
  60.         }
  61.         if($locale instanceof LocaleEntity) {
  62.             $deeplinkConfiguratorLang = str_replace('-', '_', $locale->getCode());
  63.         }
  64.         if( $deeplinkConfiguratorLang != 'de_DE'){
  65.             $deeplinkConfiguratorLang = 'en_EN';
  66.             $deeplinkConfiguratorShort = 'en';
  67.         }
  68.         if( $deeplinkConfiguratorParameters != ''){
  69.             $deeplinkConfiguratorParameters = $deeplinkConfiguratorParameters . ',' . $deeplinkConfiguratorShort;
  70.         }else{
  71.             $deeplinkConfiguratorParameters = $deeplinkConfiguratorShort;
  72.         }
  73.         $event->setParameter('deeplinkConfiguratorId', $deeplinkConfiguratorId);
  74.         $event->setParameter('deeplinkConfiguratorParameters', $deeplinkConfiguratorParameters);
  75.         $event->setParameter('deeplinkConfiguratorLang', $deeplinkConfiguratorLang);
  76.     }
  77. }