Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override twig template from plugin

Tags:

twig

shopware

I would like to override a block from a twig template from a plugin in my own plugin. I have overridden native shopware templates in the past, but I have troubles with plugin templates.

The plugin I am trying to adjust is FroshProductCompare and I am also using FroshDevelopmentHelper to easily retrieve the template I am trying to adjust.

Via the development helper, I found out that the template is located at

vendor/store.shopware.com/froshproductcompare/src/Resources/views/storefront/component/compare/section/overview.html.twig

So I created the file at

custom/plugins/ExamplePlugin/Resources/views/storefront/component/compare/section/overview.html.twig

and added

{% sw_extends "@FroshProductCompare/storefront/component/compare/section/overview.html.twig" %}

to the first line of the file.

I have also tried

{% sw_extends "@Storefront/storefront/component/compare/section/overview.html.twig" %}

I have built the storefront and cleared the cache multiple times and I have also reinstalled the plugins in different orders without success.

I found similar questions, which are not the same. E.G. How to do a template multiple inheritance in Shopware 6? But the difference here is, that the block is not native to shopware and is created by the plugin.

Any ideas?

like image 473
MweisIMI Avatar asked Oct 22 '25 06:10

MweisIMI


1 Answers

The issue is most likely due to plugin load order.

tl;dr: Older plugins (based on installation date) take precedence over newer plugins. But if you want to make sure your plugins take precedence defined the load order in your theme.

It starts with finding the right template in \Shopware\Core\Framework\Adapter\Twig\TemplateFinder::find you can see a queue is build based \Shopware\Core\Framework\Adapter\Twig\TemplateFinder::getNamespaceHierarchy which in turn gets it from \Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\NamespaceHierarchyBuilder. The method return the first found template to the order is the key.

The NamespaceHierarchyBuilder is tagged with shopware.twig.hierarchy_builder which is used to inject the following builders:

  • Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\BundleHierarchyBuilder with priority 1000
  • Shopware\Storefront\Theme\Twig\ThemeNamespaceHierarchyBuilder with priority 500

Since you want to overwrite a template from a plugin with a plugin BundleHierarchyBuilder is the interesting one (all plugins are simply symfony bundles).

BundleHierarchyBuilder is getting the bundles from the kernel (\Shopware\Core\Kernel). The kernel registers the bundles in \Shopware\Core\Kernel::registerBundles and uses an instance of \Shopware\Core\Framework\Plugin\KernelPluginLoader\KernelPluginLoader to load the plugins. The used instance is \Shopware\Core\Framework\Plugin\KernelPluginLoader\DbalKernelPluginLoader as you can see in \Shopware\Core\HttpKernel::createPluginLoader.

As described in this answer DbalKernelPluginLoader loads the plugins based on the installation date. So plugins with an older installation date will be higher up in the list in the TemplateFinder and their template will be returned.

I would actually recommend you this solution: Define the load order in your own theme.

like image 173
Daniel Avatar answered Oct 25 '25 20:10

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!