从插件覆盖树枝模板 [英] Override twig template from plugin

查看:22
本文介绍了从插件覆盖树枝模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我自己的插件中的插件覆盖来自树枝模板的块.过去我已经覆盖了本地商店软件模板,但我在使用插件模板时遇到了麻烦.

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.

我要调整的插件是 FroshProductCompare,我还使用 FroshDevelopmentHelper 轻松检索我要调整的模板.

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

所以我在

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

并添加

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

到文件的第一行.

我也试过

{% 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.

我发现了类似的问题,但并不相同.例如.如何在 Shopware 6 中进行模板多重继承?但这里的区别在于,该块不是商店软件原生的,而是由插件创建的.

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.

有什么想法吗?

推荐答案

这个问题很可能是插件加载顺序造成的.

The issue is most likely due to plugin load order.

tl;dr:旧插件(基于安装日期)优先于新插件.但是如果你想确保你的插件优先在你的主题中定义加载顺序.

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.

首先在 ShopwareCoreFrameworkAdapterTwigTemplateFinder::find 中找到正确的模板,您可以看到基于 ShopwareCoreFramework 构建的队列AdapterTwigTemplateFinder::getNamespaceHierarchy 依次从 ShopwareCoreFrameworkAdapterTwigNamespaceHierarchyNamespaceHierarchyBuilder 获取它.将找到的第一个模板返回给顺序的方法是关键.

It starts with finding the right template in ShopwareCoreFrameworkAdapterTwigTemplateFinder::find you can see a queue is build based ShopwareCoreFrameworkAdapterTwigTemplateFinder::getNamespaceHierarchy which in turn gets it from ShopwareCoreFrameworkAdapterTwigNamespaceHierarchyNamespaceHierarchyBuilder. The method return the first found template to the order is the key.

NamespaceHierarchyBuilder 带有 shopware.twig.hierarchy_builder 标记,用于注入以下构建器:

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

  • ShopwareCoreFrameworkAdapterTwigNamespaceHierarchyBundleHierarchyBuilder,优先级为 1000
  • ShopwareStorefrontThemeTwigThemeNamespaceHierarchyBuilder 优先级为 500
  • ShopwareCoreFrameworkAdapterTwigNamespaceHierarchyBundleHierarchyBuilder with priority 1000
  • ShopwareStorefrontThemeTwigThemeNamespaceHierarchyBuilder with priority 500

因为你想用一个插件 BundleHierarchyBuilder 覆盖一个插件的模板是一个有趣的(所有插件都是简单的 symfony 包).

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 正在从内核 (ShopwareCoreKernel) 获取捆绑包.内核在 ShopwareCoreKernel::registerBundles 中注册捆绑包并使用 ShopwareCoreFrameworkPluginKernelPluginLoaderKernelPluginLoader 的实例来加载插件.使用的实例是 ShopwareCoreFrameworkPluginKernelPluginLoaderDbalKernelPluginLoader,您可以在 ShopwareCoreHttpKernel::createPluginLoader 中看到.

BundleHierarchyBuilder is getting the bundles from the kernel (ShopwareCoreKernel). The kernel registers the bundles in ShopwareCoreKernel::registerBundles and uses an instance of ShopwareCoreFrameworkPluginKernelPluginLoaderKernelPluginLoader to load the plugins. The used instance is ShopwareCoreFrameworkPluginKernelPluginLoaderDbalKernelPluginLoader as you can see in ShopwareCoreHttpKernel::createPluginLoader.

this answer中所述,DbalKernelPluginLoader 根据安装日期加载插件.因此,安装日期较早的插件将在 TemplateFinder 的列表中更高,并且它们的模板将被返回.

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.

这篇关于从插件覆盖树枝模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆