14

Is there a way to add widget to the layout xml? I know about the layout updates from the backend, but I wanted to know if there is a way to add widgets the same way as static blocks?

V.S
  • 314
  • 1
  • 4
  • 14

1 Answers1

12

you can add widget via followings xml ,

<referenceContainer name="content.top">
    <block class="Magento\Catalog\Block\Category\Widget\Link" name="demoBlock" template="widget/static_block/default.phtml">
        <action method="setData">
            <argument name="id_path" xsi:type="string">category/20</argument>
            <argument name="title" xsi:type="string">Demo Category Title</argument>
            <argument name="template" xsi:type="string">category/widget/link/link_inline.phtml</argument>
        </action>
    </block>
</referenceContainer>

you need to update followings

  • referenceContainer as per you needs
  • Block Class & Template (if you require to update the template for block)
  • arguments in setData
Emizen Tech
  • 3,529
  • 1
  • 17
  • 33
  • 1
    Is there a way to specify a widget that has already been created in the backend? Maybe by using the widget id? – V.S Jul 28 '16 at 15:15
  • there is no need to do that in layout files admin widgets directly loads to selected configuration , there is configuration on each widget where you want to display that. – Emizen Tech Jul 28 '16 at 16:14
  • It works and I can set custom template as well. Thanks! – V.S Aug 08 '16 at 05:41
  • 5
    I tried this with a custom widget that needed some arguments. They weren’t send to the widget correctly. After changing to It worked like a charm. – Dennis van Schaik Nov 18 '16 at 10:30