custom/static-plugins/LnbThemeDefault/src/Subscriber/ConfigSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace Lnb\Shopware6\LnbThemeDefault\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ConfigSubscriber implements EventSubscriberInterface
  8. {
  9.     private const RESOLVE_LANGUAGE_CONFIG = [
  10.         //key in frontend extension => key in system config
  11.         'showNotificationBar' => 'notificationBarLanguageIds'
  12.     ];
  13.     public function __construct(
  14.         private SystemConfigService $systemConfigService
  15.     )
  16.     {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
  22.         ];
  23.     }
  24.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event): void
  25.     {
  26.         $languageId $event->getSalesChannelContext()->getContext()->getLanguageId();
  27.         $config = [];
  28.         foreach (self::RESOLVE_LANGUAGE_CONFIG as $key => $configKey) {
  29.             $availableLanguages $this->loadArrayConfig($configKey);
  30.             if ($availableLanguages === null) {
  31.                 $config[$key] = true;
  32.                 continue;
  33.             }
  34.             if (!in_array($languageId$availableLanguages)) {
  35.                 $config[$key] = false;
  36.                 continue;
  37.             }
  38.             $config[$key] = true;
  39.         }
  40.         $event->getPagelet()->addExtension('lnb_theme_default_language_configs', new ArrayStruct($config));
  41.     }
  42.     private function loadArrayConfig(string $key): ?array
  43.     {
  44.         $config $this->systemConfigService->get($this->buildConfigKey($key));
  45.         return is_array($config) ? $config null;
  46.     }
  47.     private function buildConfigKey(string $key): string
  48.     {
  49.         return sprintf('LnbThemeDefault.config.%s'$key);
  50.     }
  51. }