<?php
declare(strict_types=1);
namespace CioDeepConfigurator\Subscriber;
use Shopware\Core\Content\Category\Service\NavigationLoader;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\Language\LanguageEntity;
use Shopware\Core\System\Locale\LocaleEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\LandingPage\LandingPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
class TemplateSubscriber implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
private EntityRepository $languageRepository;
private EntityRepository $localeRepository;
private TranslatorInterface $translator;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepository $languageRepository,
EntityRepository $localeRepository,
TranslatorInterface $translator
) {
$this->systemConfigService = $systemConfigService;
$this->languageRepository = $languageRepository;
$this->localeRepository = $localeRepository;
$this->translator = $translator;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$deeplinkConfiguratorId = $this->systemConfigService->get('CioDeepConfigurator.config.deeplinkConfiguratorId');
$deeplinkConfiguratorParameters = $this->systemConfigService->get('CioDeepConfigurator.config.deeplinkConfiguratorParameters');
$locale = null;
$deeplinkConfiguratorLang = 'de_De';
$deeplinkConfiguratorShort = 'de';
$languageId = $event->getSalesChannelContext()->getLanguageId();
$language = $this->languageRepository->search(
(new Criteria())->addFilter(new EqualsFilter('id', $languageId)),
Context::createDefaultContext()
)->first();
if ($language instanceof LanguageEntity) {
$localeId = $language->getLocaleId();
$locale = $this->localeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('id', $localeId)),
Context::createDefaultContext()
)->first();
}
if($locale instanceof LocaleEntity) {
$deeplinkConfiguratorLang = str_replace('-', '_', $locale->getCode());
}
if( $deeplinkConfiguratorLang != 'de_DE'){
$deeplinkConfiguratorLang = 'en_EN';
$deeplinkConfiguratorShort = 'en';
}
if( $deeplinkConfiguratorParameters != ''){
$deeplinkConfiguratorParameters = $deeplinkConfiguratorParameters . ',' . $deeplinkConfiguratorShort;
}else{
$deeplinkConfiguratorParameters = $deeplinkConfiguratorShort;
}
$event->setParameter('deeplinkConfiguratorId', $deeplinkConfiguratorId);
$event->setParameter('deeplinkConfiguratorParameters', $deeplinkConfiguratorParameters);
$event->setParameter('deeplinkConfiguratorLang', $deeplinkConfiguratorLang);
}
}