Magento 2 Widgets Tutorial
Widgets are a great functionality of Magento, which is unfortunately often brushed aside by developers. This is partly because of a lack of resources which would in simple terms explain their usage and implementation to inexperienced coders.
Widgets combine the simplicity of using CMS blocks with the extensiveness of templates. They allow us to leave the configuration of a given element up to the backend user, without cluttering the database with redundant markup. For example, if the user wants to choose the location of the Call to Action button on the home page hero banner it is far easier for them to simply choose an option from a dropdown with a list of possible locations, than to struggle with markup and the WYSIWYG editor.
Widgets are best described as configurable instances of blocks (which are a part of modules).
Following the assumption that it is best to learn by doing, we will create a small widget today, which will display an image from the unsplash.com database on a page. The user adding the image will have an ability to select the category from which the image is to be pulled. This simple example will allow us to trace the process of adding and configuring the widget without delving into the complicated logic and templating.
We will begin with adding a module:
Now that the module is declared, it is time to add a block:
As you can see, the only thing we did here is set a template for our widget. However, there is nothing that would prevent us from giving the user a choice of several templates. Now we can handle the configuration of the widget. Here we can set the ID for the widget, the class of the block which the widget will use, and its name which will be visible in the backend. Additionally, in the same file we set the parameters which the widget will take. We are interested specifically in the category from which the image is to be pulled:
The only thing left to do now is adding the template which will display the image:
And that would be it. Now all that you have to do is run setup:upgrade, so that our new module is registered in the database, and add the widget where needed.