Shopware 6 - Tips, tricks and how to's

How to set up default values for Shopware 6 plugin config

This article will show you, how to set up default values, that you want the user to see in your plugin’s configuration, after he installs and activates it. If you are new to the Shopware 6 plugin configuration, then I suggest you read my article How to create config for your Shopware 6 plugin first, to get some basic information with examples. This article expands the knowledge from the previous one and also builds on some information from the article Accessing your plugin’s classes during its installation and activation.

Set the default values in config.xml

The easiest way to set up the default value of a configuration variable is to put it to an appropriate XML element in the config.xml file:

However, there are certain limits to this, at least in Shopware version 6.4, which is the most recent version at the time of the writing of this text.

Internally, Shopware 6 uses a JSON field, named ‘configuration_value’ in the ‘system_config’ table for storing the plugin config values. That is a fine choice, because it gives a lot of flexibility and allows to save nearly anything in there. However, if we take a look at how the default value from the config.xml file gets saved, it is limited by the method ‘phpize’ from the XmlUtils library, used by Shopware for this purpose.

This is how the method ‘saveConfig’ from the class ‘vendor/shopware/core/System/SystemConfig/SystemConfigService.php’ looks like:

If you want to use a component in the plugin configuration page, whose value is an array, the config.xml way method is not usable. Luckily, there is a viable alternative.

Set the default values in the plugin’s main class

When do you want to set the defaults? The answer would usually be: during installation or activation of the plugin. And rightfully so. The user can access the plugin configuration from the Shopware backend only after the plugin is properly installed and activated. So if we could not set something up in the config.xml file, we have another chance to do it, when he installs or activates our plugin.

So let’s take an example of the ‘entity multi select’ component, that contains the countries. Here is how it looks visually in the plugin configuration:

And this is the source code behind it:

This component allows us to select multiple countries from the country repository. Similarly, we could create such a component for products and other entities, if we specified them in the <entity> element.

Now we want to pre-select some countries. If we try to use <defaultValue> in the config.xml file, we are going to be disappointed. It will not work, because of the fore mentioned limitations. To the rescue come the ‘install’ and ‘activate’ methods of our main plugin class!

As I have explained in the Shopware 6 plugin programming tutorial, every plugin for Shopware 6 must contain the main class, that is named similarly. This class extends the core class Plugin, which in turn extends the class Bundle etc. For us it means, that we can access the dependency injection container. Therefore we have access to the public classes, including the one, that we need for setting the config values. Yes, it is the ‘SystemConfigService’ class!

Here is the code for getting the SystemConfigService during installation and activation of the plugin:

Now we can use the method ‘set’ to set the default values for our country multiselect, but before that, we have to know, which values we want to put there. Putting in the names of the countries or their ISO codes will not work. As usual in Shopware 6, every record in an entity table has a unique ID, called UUID. This is not your classical numerical autoincremented ID, so it is bit trickier to get it from the database. We can do it like this:

Please notice the ‘LOWER’ keyword in the MySQL query. It converts the characters in the UUID string to lower case. In same places in Shopware 6, this conversion is not necessary. However, in this case, it definitely is – otherwise the UUIDs would not get matched.

So now we have the IDs of the countries, that we want to pre-select in our multiselect component. At last, we can set them as default values during the plugin installation:

If you install your plugin now and activate it, you will see the pre-selected countries in the plugin configuration page. Hooray! 🙂

If you want to delve more into the topic of Shopware 6 plugin installation, uninstallation, activation and deactivation, I really recommend reading my article Accessing your plugin’s classes during its installation and activation. In any case, thank you for reading and if you have any suggestions or additional questions, please leave them in the comments below.