Skip to main content

Shopware6 - Storefront - Access plugin config values in Twig

Category: Shopware
shopware-logo

Just an example how to read configuration values and access them via Twig in Shopware6 Storefront-Templates

Subscriber
Read the configuration related to the active saleschannel and assign it as extension to Twig.

<?php
declare(strict_types=1);

namespace contribName\PluginName\Subscriber;

use contribName\PluginName\Structs\StorefrontPageData;
use Shopware\Core\System\SystemConfig\Exception\InvalidDomainException;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class Storefront implements EventSubscriberInterface
{
    /**
     * @var SystemConfigService
     */
    private $systemConfigService;

    /**
     * Storefront constructor.
     * @param SystemConfigService $systemConfigService
     */
    public function __construct(SystemConfigService $systemConfigService)
    {
        $this->systemConfigService = $systemConfigService;
    }

   /**
   * @return array
   */
    public static function getSubscribedEvents(): array
    {
        return [
            FooterPageletLoadedEvent::class => 'onFooterLoaded'
        ];
    }

  /**
   * @param FooterPageletLoadedEvent $event
   * @throws InvalidDomainException
   */
  public function onFooterLoaded(FooterPageletLoadedEvent $event) :void
  {
      $config = $this->systemConfigService->getDomain('contribNamePluginName.config', $event->getSalesChannelContext()->getSalesChannel()->getId(), true );
      $configValues = new StorefrontPageData($config);

      $event->getPagelet()->addExtension('contribNamePluginName', $configValues);
  }   
}

Struct
Contemporary Shopware 6 does not support default config values (EA 1), so we'll handle this in our struct.

<?php
declare(strict_types=1);

namespace contribName\PluginName\Structs;

use Shopware\Core\Framework\Struct\Struct;

class StorefrontPageData extends Struct {

    /**
     * @var array
     */
    protected $config = [];

    /**
     * @var string
     */
    private $configPath = 'contribNamePluginName.config.';

    /**
     * StorefrontPageData constructor.
     * @param array $pluginConfig
     */
    public function __construct(array $pluginConfig)
    {
        //temporary till sw config does support default values
        $this->setValues($pluginConfig, 'padding', 15);
        $this->setValues($pluginConfig, 'fontSize', 14);
    }

    /**
     * @return mixed
     */
    public function getConfig() : array
    {
        return $this->config;
    }

    /**
     * @param array $config
     */
    public function setConfig(array $config): void
    {
        $this->config = $config;
    }

    /**
     * @param array $pluginConfig
     * @param string $key
     * @param null $default
     */
    public function setValues(array $pluginConfig, string $key, $default = null): void
    {
        if(array_key_exists($this->configPath . $key, $pluginConfig)) {
            $this->config[$key] = $pluginConfig[$this->configPath . $key];
        }
        else {
            $this->config[$key] = $default;
        }
    }
}

Twig

{{  page.footer.extensions["contribNamePluginName"].config["value"] }};