You are seeking the way of creating Widgets in Magento 2?
There are some methods which will help you to build Magento 2 Widgets listed are:
- File Setup/Our module
- Widgets declaration file
- Lets build the model file
- Lets build the block file
- build a temple file
In our previous tutorial we have seen about how to use Magento 2 CMS(content Management System), Now we will go down in aspect with the Magento 2 Widget inside it.
Magento 2 Widgets will permit the site admin to add admin contact information such as full Name, age, gender…etc. moreover you can create Magento 2 widget through widget instance or include in the editor part of CMS page->block. We will make use of common field types in Magento 2 widgets such as text and dropdown.
- File setup/Our module
- Widget declaration File
- let’s create the model file
- let’s create the block file
- create the template file
I guess that you’ll well-known about the structure of Magento 2 module. So let’s start:
First, let’s see the key structure which we’ll need to execute for our customer Magento 2 widgets.
file setup for our module
a widget declaration file used to declare widgets information and parameters.
File setup/our module
First we have to build a module setup file. Build a file
and paste the following contents in that file. We have used “webnexs” as our component retailer name and “customer widget” as our component name.
Widget declaration file
Build the widget file
- Declare our widget with our only one of its kind identification is webnexs customwidget and the class feature is used to plot the widget file.
- The field description will show some description, introduce about the module when created.
- We need to state all the choice of component inside the field label “parameters”.
- And the source model element plot to the model file
- where we will get our choice for the drop-down.
Let’s build the model file
lets build the block file
Which is declared above code? In this file, we assign custom template file inside _toHtml() method .
Let’s build the template file
Which will illustrate all the widgets data on site?
- Now, you need to clear all the caches from the backend of magento or delete a folder var/cache.
- Then, go to admin page->content->pages and attach new page using attach new page button. Then click widget icon in content tab and you need to load all information for all field.
- Save CMS page and go to the front end of page to ensure your widgets.
Last Update: July 24, 2018