Use Plugin Configuration
The Add a Plugin Configuration Guide shows how to define configuration options in your plugins. This guide helps you to use them in your plugin, showing you how to read plugin configuration values in PHP, Administration JavaScript, and Storefront code.
Prerequisites
- First, review the Plugin Base Guide
- Then define a plugin configuration field in Add plugin configuration
- Get familiar with the Listening to events guide, as in this example the configuration is read inside of a subscriber
The example plugin includes a subscriber that listens to the product.loaded event and is called every time a product is loaded.
php
// <plugin root>/src/Subscriber/MySubscriber.php
<?php declare(strict_types=1);
namespace Swag\BasicExample\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
];
}
public function onProductsLoaded(EntityLoadedEvent $event): void
{
// Do stuff with the product
}
}For this guide, a very small plugin configuration file is available as well:
xml
<!-- <plugin root>/src/Resources/config/config.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/System/SystemConfig/Schema/config.xsd">
<card>
<title>Minimal configuration</title>
<input-field>
<name>example</name>
</input-field>
</card>
</config>Just a simple input field with the technical name example. This will be necessary in the next step.
Reading the configuration
Use the tabs below depending on where you need the value: PHP (services, subscribers), Administration (JavaScript) (custom Admin modules), or Storefront (Twig / theme JS).