vendor/store.shopware.com/llamafancysearchsw6/src/Subscriber/ThemeVariablesSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LlamaFancySearchSw6\Subscriber;
  3. use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  7. class ThemeVariablesSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var SystemConfigService
  11.      */
  12.     protected $systemConfig;
  13.     // add the `SystemConfigService` to your constructor
  14.     public function __construct(SystemConfigService $systemConfig)
  15.     {
  16.         $this->systemConfig = $systemConfig;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
  22.         ];
  23.     }
  24.     public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
  25.     {
  26.         $configFields = $this->systemConfig->get('LlamaFancySearchSw6.config', $event->getSalesChannelId());
  27.         foreach($configFields as $key => $value) {
  28.             // Convert `customVariableName` to `custom-variable-name`
  29.             $variableName = str_replace('_', '-', (new CamelCaseToSnakeCaseNameConverter())->normalize($key));
  30.             // manually set empty vars to '0' to avoid theme compiling exceptions
  31.             if (empty($value)) {
  32.                 $event->addVariable($variableName, '0');
  33.             }else{
  34.                 $event->addVariable($variableName, strval($value));
  35.             }
  36.         }
  37.     }
  38. }