Search code examples
shopwareshopware6

[Shopware6]: Extend the configuration of another Plugin


I have successfully extended multiple Plugins by using decorators and symfonys dependency injection.

But right now, i need to extend or overwrite the config.xml of another Plugin. How could i achieve that?


Solution

  • Updated answer

    I think I read your question wrong. In case you want to alter not the output of config values but the config itself, you may decorate the ConfigReader service.

    <service id="MyPlugin\Decorator\ConfigReaderDecorator" decorates="Shopware\Core\System\SystemConfig\Util\ConfigReader">
        <argument type="service" id="MyPlugin\Decorator\ConfigReaderDecorator.inner"/>
    </service>
    
    class ConfigReaderDecorator extends ConfigReader
    {
        private ConfigReader $decorated;
    
        public function __construct(ConfigReader $configReader)
        {
            $this->decorated = $configReader;
        }
    
        public function getConfigFromBundle(Bundle $bundle, ?string $bundleConfigName = null): array
        {
            $config = $this->decorated->getConfigFromBundle($bundle, $bundleConfigName);
    
            if ($bundle->getName() === 'NameOfThePlugin') {
                // overwrite or extend the config of another plugin
                // var_dump($config);
    
                // add a new card with a new field to the existing config
                $config[] = [
                    'title' => [
                        'en-GB' => 'A new card injected',
                    ],
                    'elements' => [
                        [
                            'component' => 'sw-text-field',
                            'label' => 'A new field injected',
                            'name' => 'nameOfInjectedField',
                        ],
                    ],
                ];
            }
    
            return $config;
        }
    }
    

    Old answer

    Decorate the SystemConfigLoader service.

    <service id="MyPlugin\Decorator\SystemConfigLoaderDecorator" decorates="Shopware\Core\System\SystemConfig\SystemConfigLoader">
        <argument type="service" id="MyPlugin\Decorator\SystemConfigLoaderDecorator.inner"/>
    </service>
    
    class SystemConfigLoaderDecorator extends AbstractSystemConfigLoader
    {
        private AbstractSystemConfigLoader $decorated;
    
        public function __construct(AbstractSystemConfigLoader $decorated)
        {
            $this->decorated = $decorated;
        }
    
        public function getDecorated(): AbstractSystemConfigLoader
        {
            return $this->decorated;
        }
    
        public function load(?string $salesChannelId): array
        {
            $config = $this->getDecorated()->load($salesChannelId);
    
            // overwrite the config values
            $config['NameOfThePlugin']['config']['pluginConfigFieldName'] = 'foobar';
    
            return $config;
        }
    }