7

There is widget.xml in CatalogWidget module. Inside the widget.xml there is

        <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>

What is a proper way to change the template file for the widget?

9

you can extend the widget in your module.

To do that your module has to depend on the module where the original widget is defined (see sequence in the module.xml).

Then you create a widget.xml in your etc dir.

The key is, not to use the widget.xsd (there are many fields required that you do not need for extending) but the widget_file.xsd.

Here is an example to add a template to the static block 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_file.xsd">
    <widget id="cms_static_block">
        <parameters>
            <parameter name="template" xsi:type="select">
                <options>
                    <option name="option_id" value="Your_Module::view/form.phtml">
                        <label translate="true">Your Template</label>
                    </option>
                </options>
            </parameter>
        </parameters>
    </widget>
</widgets>

Here is another example with a container config, to allow the template for specific containers:

<?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_file.xsd">
    <widget id="products_list">
        <parameters>
            <parameter name="template" xsi:type="select">
                <options>
                    <option name="slider" value="Vendor_Module::product/widget/content/slider.phtml">
                        <label translate="true">Products Slider Template</label>
                    </option>
                </options>
            </parameter>
        </parameters>
        <containers>
            <container name="content">
                <template name="slider" value="slider" />
            </container>
            <container name="content.top">
                <template name="slider" value="slider" />
            </container>
            <container name="content.bottom">
                <template name="slider" value="slider" />
            </container>
        </containers>
    </widget>
</widgets>
  • 1
    I’ve tried creating a module that uses your widget.xml file and I get this error Invalid Document Element 'label': This element is not expected. Line: 34 The line 34 this error is referring to is in vendor/magento/module-catalog-widget/etc/widget.xml Can you try creating a module and implementing your solution to include a template in that widget? I think you may get the same error as me. If you do I will submit an issue on github. If not at least I will know I am doing something wrong. – Nathan Toombs Apr 29 '16 at 23:27
  • 1
    hm, we are using this in an actual project for a product slider and it still works. Are you sure you are using the widget_file.xsd and that your xml structure is correct? Maybe try validateing your widget.xml against the widget_file.xsd. – David Verholen Apr 30 '16 at 5:14
  • It works for cms_static_block but not for the products_list widget. If you changed the id parameter on the widget node to products_list you will likely see the same error I am seeing. – Nathan Toombs May 2 '16 at 14:19
  • 1
    actually, in our module, whe are extending the products_list widget to create a really lightweight product slider module (added as example to my answer). I don't think it will solve your problem, but we had to add the container config so that the template is allowed for the containers defined in the original widget – David Verholen May 2 '16 at 16:54
  • Looks like the container config missing from my widget.xml file was the problem. Thanks! – Nathan Toombs May 2 '16 at 20:31
6

You can replace the grid.phtml where you call the widget

{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" title="Our Products" products_count="8" template="Namespace_Modulename::product/widget/content/grid.phtml" }}
2

Create a custom theme and copy the .phtml file to it, and make your changes.

app/design/frontend/<package>/<theme>/Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml

  • Ok, but Im looking a solution that I can override the phtml inside my custom modul. To override in some xml file or something. Is it possible? – ciemin Jan 19 '16 at 9:27
  • Magento will automatically load your phtml file from your module if it is placed in the correct folder, as I show above. There's no XML changes needed to override a phtml file. – andyjv Jan 19 '16 at 12:52
  • But Im looking other way then overriding that phtml in the way you mentioned. I need to do that in the scope of my custom module, I dont want to put any files to a theme. – ciemin Jan 20 '16 at 8:24
  • Template files for widgets go in <module>/view/frontend/templates/ and you would specify the file in your XML the same way CatalogWidget does. Even if you are extending CatalogWidget and your template is <module>/view/frontend/templates/product/widget/content/grid.phtml, the file in your custom module directory would take precedence. – andyjv Jan 20 '16 at 11:35
  • "you would specify the file in your XML the same way CatalogWidget does" what do you mean by that? what XML? Copying template to my module with same path as in Magento_CatalogWidget and adding sequence node to module.xml simply doesnt work. – ciemin Jan 21 '16 at 10:10
2

I had the same error: Invalid Document Element 'label': This element is not expected. Line: 34 with products_list This worked for me (Magento 2.0.4) widget.xml file in your custom module:

<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="products_list">
        <label translate="true">Catalog Products List</label>
        <description translate="true">List of Products</description>
        <parameters>
            <parameter name="template" xsi:type="select">
                <label translate="true">Template</label>
                <options>
                    <option name="slider" value="Vendor_Modul::product/widget/content/slider.phtml">
                        <label translate="true">Slider</label>
                    </option>
                </options>
            </parameter>
        </parameters>
        <containers>
            <container name="content">
                <template name="slider" value="slider" />
            </container>
            <container name="content.top">
                <template name="slider" value="slider" />
            </container>
            <container name="content.bottom">
                <template name="slider" value="slider" />
            </container>
        </containers>
    </widget>
</widgets>
  • Thanks. In my case I had to add only the label tag (widgets > widget > parameters > parameter > label) and then it worked. So the containers is not required. – Zsolti Jul 11 '18 at 8:57
-1

Ive created a preference for the widgets block class which makes the template path relative to the modules directory.

di.xml:

<preference
    for="Magento\CatalogWidget\Block\Product\ProductsList"
    type="Vendor\Module\Block\CatalogWidget\Product\ProductsList" />

Class:

use Vendor\Module\Block\CatalogWidget\Product;

class ProductList extends \Magento\CatalogWidget\Block\Product\ProductList {

}

Now the template in
Vendor/Module/view/frontend/templates/product/widget/content/grid.phtml is used instead of Magento/CatalogWidget/view/frontend/templates/product/widget/content/grid.phtml

  • I tried with this step but its not working. – Abhimanyu Singh Sep 4 '19 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.