I need to create widget category link via Upgrade. How to do this?

  • Creating a widget cannot be done in setup files, however you can create module for it and when it installs widget will appear in admin. Please let me know if you want code for that. – Vivek Kumar Aug 30 '18 at 14:59
  • it would be great! Thx – dukir Aug 30 '18 at 15:53

Custom widget can not be created alone with a Install or Upgrade script. You need to create a module for it.

Below are the simple steps to create a widget via a module:

  1. Create app/code/Stack/CustomWidget/registration.php and paste below code in it:
  1. Create app/code/Stack/CustomWidget/module.xml and paste below code in it:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Stack_CustomWidget" setup_version="1.0.0"/>
  1. Next, you’ll need to create a widget.xml configuration file under app/code/Stack/CustomWidget/etc/ folder with the following code:
<?xml version="1.0" ?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget class="Stack\CustomWidget\Block\Widget\Samplewidget" id="stack_customwidget_samplewidget">
        <label>Stack Sample Widget</label>
             <parameter name="widgettitle" sort_order="10" visible="true" xsi:type="text">
             <parameter name="widgetcontent" sort_order="20" visible="true" xsi:type="textarea">

In the <widget> tag, I have declared a block class, Stack\CustomWidget\Block\Widget\Samplewidget that instructs our widget to use the particular template.

  1. Now create Samplewidget.php in app/code/Stack/CustomWidget/Block/Widget/ directory with the following code:
namespace Stack\CustomWidget\Block\Widget;

use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;

class Samplewidget extends Template implements BlockInterface
    protected $_template = "widget/samplewidget.phtml";
  1. Finally, create a widget template file samplewidget.phtml in app/code/Stack/CustomWidget/view/frontend/templates/widget with the following code:
<?php if($block->getData('widgettitle')): ?>
            <h2 class='stack-title'><?php echo $block->getData('widgettitle'); ?></h2>
<?php endif; ?>
<?php if($block->getData('widgetcontent')): ?>
            <h2 class='stack-content'><?php echo $block->getData('widgetcontent'); ?></h2>
<?php endif; ?>

Here, as you can see that I have picked up the value from fields by calling $block->getData('parameter'); where the parameters are widgettitle and widgetcontent.

Now, login to your Magento 2 Admin panel and navigate to Content > Pages to see the widget in action.


You can find a pretty good tutorial for creating a widget in custom module by inchoo here.

Here is also same module on github so you can customise it as your need.

Please let me know if have any confusion.

  • i should to do my own category link widget, how to do this? need create 4 category link widgets for different blocks – dukir Aug 31 '18 at 8:36

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.