在主题内扩展Magento小部件布局 [英] Extending a Magento widget layout within a theme
问题描述
我正在使用Magento 2的内置目录产品列表"窗口小部件将特色产品窗口小部件添加到网站的主页.我希望做的是扩展在 vendor/magento/module-catolog/widget/etc/widget.xml
中找到的小部件的布局文件,以便在添加此模板时可以添加另一个模板选项使用 WYSIWIG
编辑器在cms块中插入窗口小部件.
I'm adding a featured products widget to the home page of a site using Magento 2's built-in Catalog Products List widget. What I'm hoping to do is to to extend the widget's layout file found in vendor/magento/module-catolog/widget/etc/widget.xml
so that I can add another template option when adding this widget in a cms block with the WYSIWIG
editor.
现在,我在 app/design/frontend/Vendor/theme
中有一个主题,我尝试将自己的 widget.xml
文件放在app/design/frontend/Vendor/theme/Magento_CatalogWidget/widget.xml
,但这似乎无法覆盖原始的 widget.xml
文件.
Right now, I have a theme located in app/design/frontend/Vendor/theme
, and I tried putting my own widget.xml
file at app/design/frontend/Vendor/theme/Magento_CatalogWidget/widget.xml
, but it seems that this fails to override the original widget.xml
file.
这是我的代码:
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
<widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
<label translate="true">Catalog Products List</label>
<description translate="true">List of Products</description>
<parameters>
<parameter name="template" xsi:type="select" required="true" visible="true">
<label translate="true">Template</label>
<options>
<option name="default" value="product/widget/content/grid.phtml" selected="true">
<label translate="true">Products Grid Template</label>
</option>
<option name="default" value="product/widget/content/alternate-grid.phtml" selected="true">
<label translate="true">Alternate Products Grid Template</label>
</option>
</options>
</parameter>
</parameters>
</widget>
</widgets>
我正在尝试在template参数内添加一个选项,以便在插入小部件时可以选择替代产品网格模板".
I'm trying to add an option within the template parameter so that I can select an "Alternate Products Grid Template" when inserting the widget.
这是来自 magento-catalog-widget
的原始 .xml
文件:
Here's the original .xml
file from magento-catalog-widget
:
<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
<widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
<label translate="true">Catalog Products List</label>
<description translate="true">List of Products</description>
<parameters>
<parameter name="title" xsi:type="text" required="false" visible="true">
<label translate="true">Title</label>
</parameter>
<parameter name="show_pager" xsi:type="select" visible="true"
source_model="Magento\Config\Model\Config\Source\Yesno">
<label translate="true">Display Page Control</label>
</parameter>
<parameter name="products_per_page" xsi:type="text" required="true" visible="true">
<label translate="true">Number of Products per Page</label>
<depends>
<parameter name="show_pager" value="1" />
</depends>
<value>5</value>
</parameter>
<parameter name="products_count" xsi:type="text" required="true" visible="true">
<label translate="true">Number of Products to Display</label>
<value>10</value>
</parameter>
<parameter name="template" xsi:type="select" required="true" visible="true">
<label translate="true">Template</label>
<options>
<option name="default" value="product/widget/content/grid.phtml" selected="true">
<label translate="true">Products Grid Template</label>
</option>
</options>
</parameter>
<parameter name="cache_lifetime" xsi:type="text" visible="true">
<label translate="true">Cache Lifetime (Seconds)</label>
<description translate="true">86400 by default, if not set. To refresh instantly, clear the Blocks HTML Output cache.</description>
</parameter>
<parameter name="condition" xsi:type="conditions" visible="true" required="true" sort_order="10"
class="Magento\CatalogWidget\Block\Product\Widget\Conditions">
<label translate="true">Conditions</label>
</parameter>
</parameters>
<containers>
<container name="content">
<template name="grid" value="default" />
</container>
<container name="content.top">
<template name="grid" value="default" />
</container>
<container name="content.bottom">
<template name="grid" value="default" />
</container>
</containers>
</widget>
</widgets>
我知道,如果我成功扩展了文件,这将起作用,因为我已经尝试编辑原始文件,并且在admin中添加小部件时看到了我的新选项.显然,这不是最佳实践,我希望仅在自定义主题内扩展widget.xml文件.
I know that this will work if I successfully extend the file since I already tried editing the original file and saw my new option when adding the widget in the admin. Obviously that's not best practice, and I would like the widget.xml file to be extended only within my custom theme.
请注意,我的模板放置在 app/design/frontend/Vendor/theme/Magento_CatalogWidget/templates/product/widget/content
内,其中我同时拥有 grid.phtml
和 alternate-grid.phtml
.我没有在 magento_catalog_widget
目录中放置 alternate-grid.phtml
,但是当更改原始的 widget.xml
文件时,我可以访问它在该目录中(在我的主题之外).这就是为什么我非常有信心,这个问题是覆盖小部件的布局文件的原因.
Note that my templates are placed within app/design/frontend/Vendor/theme/Magento_CatalogWidget/templates/product/widget/content
, where I have both grid.phtml
and alternate-grid.phtml
. I did not place alternate-grid.phtml
within the magento_catalog_widget
directory, yet I was able to access it when changing the original widget.xml
file in that directory (outside of my theme). That's why I'm fairly confident that this problem is a matter of overwriting the layout file for the widget.
如果任何人都可以回答这个问题,我认为这对其他开发人员也很有帮助,因为这将使开发人员可以自定义任何Magento内置小部件中的选项,而不会干扰基本框架.
If anyone can answer this, I think it could be very helpful to other developers as well since this would allow a developer to customize the options within any Magento built-in widget without interfering with the base framework.
推荐答案
我知道有些晚了,但我只是想出了办法,也许会对别人有所帮助.
I know it's a bit late but I just figured out how to do this and maybe it will help someone.
文件 app/design/frontend/Vendor/theme/etc/widget.xml
应该如下所示:
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
<widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
<label translate="true">Catalog Products List</label>
<description translate="true">List of Products</description>
<parameters>
<parameter name="template" xsi:type="select" required="true" visible="true">
<label translate="true">Template</label>
<options>
<option name="default" value="product/widget/content/grid.phtml">
<label translate="true">Products Grid Template</label>
</option>
<option name="alternate_grid" value="product/widget/content/alternate-grid.phtml">
<label translate="true">Alternate Grid Template</label>
</option>
</options>
</parameter>
</parameters>
<containers>
<container name="content">
<template name="grid" value="default"/>
<template name="alternate_grid" value="alternate_grid"/>
</container>
<container name="content.top">
<template name="grid" value="default"/>
<template name="alternate_grid" value="alternate_grid"/>
</container>
<container name="content.bottom">
<template name="grid" value="default"/>
<template name="alternate_grid" value="alternate_grid"/>
</container>
</containers>
</widget>
模板文件应位于 app/design/frontend/Vendor/theme/Magento_CatalogWidget/templates/product/widget/content/alternate-grid.phtml
并且不要忘记在创建/编辑module.xml之后清除缓存
And don't forget to clear cache after creating/editing module.xml
这篇关于在主题内扩展Magento小部件布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!