<?php declare(strict_types=1);
namespace LlamaFancySearchSw6\Subscriber;
use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class ThemeVariablesSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
protected $systemConfig;
// add the `SystemConfigService` to your constructor
public function __construct(SystemConfigService $systemConfig)
{
$this->systemConfig = $systemConfig;
}
public static function getSubscribedEvents(): array
{
return [
ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
];
}
public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
{
$configFields = $this->systemConfig->get('LlamaFancySearchSw6.config', $event->getSalesChannelId());
foreach($configFields as $key => $value) {
// Convert `customVariableName` to `custom-variable-name`
$variableName = str_replace('_', '-', (new CamelCaseToSnakeCaseNameConverter())->normalize($key));
// manually set empty vars to '0' to avoid theme compiling exceptions
if (empty($value)) {
$event->addVariable($variableName, '0');
}else{
$event->addVariable($variableName, strval($value));
}
}
}
}