Use plugin configuration
In our guide on how to add a plugin configuration, you can learn how to provide this possibility to use configuration options in your plugins. This guide will aid you on how to then use this configuration in your plugin.

Prerequisites

In order to add a plugin configuration, you sure need to provide your plugin first. However, you won't learn to create a plugin in this guide. Head over to our plugin base guide to create your plugin first. It is also recommended to know how to setup a plugin configuration in the first instance. In this example, the configurations will be read inside of a subscriber, so knowing the Listening to events guide will also be helpful.

Overview

The plugin in this example already knows a subscriber, which listens to the product.loaded event and therefore will be called every time a product is loaded.
<plugin root>/src/Subscriber/MySubscriber.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Subscriber;
4
​
5
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Shopware\Core\Content\Product\ProductEvents;
8
​
9
class MySubscriber implements EventSubscriberInterface
10
{
11
public static function getSubscribedEvents(): array
12
{
13
return [
14
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
15
];
16
}
17
​
18
public function onProductsLoaded(EntityLoadedEvent $event): void
19
{
20
// Do stuff with the product
21
}
22
}
Copied!
For this guide, a very small plugin configuration file is available as well:
<plugin root>/src/Resources/config/config.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/System/SystemConfig/Schema/config.xsd">
4
​
5
<card>
6
<title>Minimal configuration</title>
7
<input-field>
8
<name>example</name>
9
</input-field>
10
</card>
11
</config>
Copied!
Just a simple input field with the technical name example. This will be necessary in the next step.

Reading the configuration

Let's get to the important part. Reading the plugin configuration is based on the Shopware\Core\System\SystemConfig\SystemConfigService. This service is responsible for reading all configs from Shopware 6, such as the plugin configurations.
Inject this service into your subscriber using the DI container.
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
​
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
​
7
<services>
8
<service id="Swag\BasicExample\Subscriber\MySubscriber">
9
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
10
<tag name="kernel.event_subscriber"/>
11
</service>
12
</services>
13
</container>
Copied!
Note the new argument being provided to your subscriber. Now create a new field in your subscriber and pass in the SystemConfigService:
<plugin root>/src/Subscriber/MySubscriber.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Subscriber;
4
​
5
...
6
use Shopware\Core\System\SystemConfig\SystemConfigService;
7
​
8
class MySubscriber implements EventSubscriberInterface
9
{
10
/**
11
* @var SystemConfigService
12
*/
13
private $systemConfigService;
14
​
15
public function __construct(SystemConfigService $systemConfigService)
16
{
17
$this->systemConfigService = $systemConfigService;
18
}
19
​
20
public static function getSubscribedEvents(): array
21
{
22
...
23
}
24
...
25
}
Copied!
So far, so good. The SystemConfigService is now available in your subscriber.
This service comes with a get method to read the configurations. The first idea would be to simply call $this->systemConfigService->get('example') now, wouldn't it? Simply using the technical name you've previously set for the configuration.
But what would happen, if there were more plugins providing the same technical name for their very own configuration field? How would you access the proper field, how would you prevent plugin conflicts?
That's why the plugin configurations are always prefixed. By default, the pattern is the following: <BundleName>.config.<configName> Thus, it would be SwagBasicExample.config.example here.
<plugin root>/src/Subscriber/MySubscriber.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Subscriber;
4
​
5
...
6
​
7
class MySubscriber implements EventSubscriberInterface
8
{
9
...
10
public function onProductsLoaded(EntityLoadedEvent $event): void
11
{
12
$exampleConfig = $this->systemConfigService->get('SwagBasicExample.config.example');
13
}
14
}
Copied!
Last modified 2mo ago