{"id":7265,"date":"2019-09-23T10:35:03","date_gmt":"2019-09-23T05:05:03","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7265"},"modified":"2020-06-05T09:57:00","modified_gmt":"2020-06-05T04:27:00","slug":"magento-2-create-custom-widget","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-create-custom-widget\/","title":{"rendered":"Magento 2 &#8211; How to Create Custom Widget"},"content":{"rendered":"<p>In this post I am going to explain how to create custom widget in magento 2.<\/p>\n<p>First we will create the widget configuration file widget.xml, that contains all widget fields. So, create the widget.xml file with following content.<\/p>\n<p>File Path: etc\/widget.xml<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;widgets xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Widget:etc\/widget.xsd\"&gt;\r\n    &lt;widget id=\"my_module_widget\" class=\"Ht\\Mymodule\\Block\\Widget\\Mymodule\"&gt;\r\n        &lt;label translate=\"true\"&gt;Ht Mymodule Widget&lt;\/label&gt;\r\n        &lt;description&gt;Ht Mymodule Widget Description&lt;\/description&gt;\r\n        &lt;parameters&gt;\r\n            &lt;parameter name=\"text_field\" xsi:type=\"text\" required=\"false\" visible=\"true\"&gt;\r\n                &lt;label translate=\"true\"&gt;Text Field&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n            &lt;parameter name=\"dropdown_field\" xsi:type=\"select\" visible=\"true\" source_model=\"Ht\\Mymodule\\Model\\Source\\Dropdownoptions\"&gt;\r\n                &lt;label translate=\"true\"&gt;Dropdown Field&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n            &lt;parameter name=\"yes_no_field\" xsi:type=\"select\" visible=\"true\" source_model=\"Magento\\Config\\Model\\Config\\Source\\Yesno\"&gt;\r\n                &lt;label translate=\"true\"&gt;Yes\/No Field&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n        &lt;\/parameters&gt;\r\n    &lt;\/widget&gt;\r\n&lt;\/widgets&gt;<\/pre>\n<p>&#8220;Dropdown Field&#8221; is a select box and we have defined a custom source: Ht\\Mymodule\\Model\\Source\\Dropdownoptions<\/p>\n<p>So, we need to create the source file as well.<\/p>\n<p>File path: app\/code\/Ht\/Mymodule\/Model\/Source\/Dropdownoptions.php<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Model\\Source;\r\n\r\nclass Dropdownoptions implements \\Magento\\Framework\\Option\\ArrayInterface \r\n{\r\n    public function toOptionArray() \r\n    {\r\n        return [\r\n            ['value' =&gt; '1', 'label' =&gt; __('Option 1')],\r\n            ['value' =&gt; '2', 'label' =&gt; __('Option 2')]\r\n        ];\r\n    }\r\n}<\/pre>\n<p>Next step is to create a custom block that our widget will use.<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/Block\/Widget\/Mymodule.php<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Block\\Widget;\r\n\r\nuse Magento\\Framework\\View\\Element\\Template;\r\nuse Magento\\Widget\\Block\\BlockInterface;\r\n\r\nclass Mymodule extends Template implements BlockInterface {\r\n\r\n    protected $_template = \"widget\/mymodule.phtml\";\r\n\r\n}<\/pre>\n<p>In block file we have added template file path for widget. In this template file you can get widget option&#8217;s values.<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/view\/frontend\/templates\/widget\/mymodule.phtml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nif ($block-&gt;getData('text_field')) {\r\n    echo $block-&gt;getData('text_field') . '&lt;br\/&gt;';\r\n}\r\n\r\nif ($block-&gt;getData('dropdown_field')) {\r\n    echo $block-&gt;getData('dropdown_field') . '&lt;br\/&gt;';\r\n}\r\n\r\nif ($block-&gt;getData('yes_no_field')) {\r\n    echo $block-&gt;getData('yes_no_field') . '&lt;br\/&gt;';\r\n}<\/pre>\n<p>We done with the widget. So now flush the cache and check for the widget.<\/p>\n<p>Login to Admin panel and Go to Content &gt; Pages\/Blocks. And add new or edit existing CMS page and CMS static block.<\/p>\n<p>In Content tab, click on &#8220;Insert Widget&#8230;&#8221; button.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7266\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/insert-widget.png\" alt=\"Magento 2 - How to Create Custom Widget\" width=\"1012\" height=\"306\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/insert-widget.png 1012w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/insert-widget-300x91.png 300w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/insert-widget-768x232.png 768w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/insert-widget-800x242.png 800w\" sizes=\"(max-width: 1012px) 100vw, 1012px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You will see the &#8220;Ht Mymodule Widget&#8221; in widget list. Select the widget and add\/select values to fields.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7267\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/select-widget.png\" alt=\"Magento 2 - How to Create Custom Widget\" width=\"893\" height=\"461\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/select-widget.png 893w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/select-widget-300x155.png 300w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/select-widget-768x396.png 768w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/select-widget-800x413.png 800w\" sizes=\"(max-width: 893px) 100vw, 893px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7268\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/widget-options.png\" alt=\"Magento 2 - How to Create Custom Widget\" width=\"1243\" height=\"445\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/widget-options.png 1243w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/widget-options-300x107.png 300w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/widget-options-768x275.png 768w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/widget-options-1024x367.png 1024w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/widget-options-800x286.png 800w\" sizes=\"(max-width: 1243px) 100vw, 1243px\" \/><\/p>\n<p>You will get code like like below in CMS page or static block.<\/p>\n<pre class=\"lang:default decode:true \">{{widget type=\"Ht\\Mymodule\\Block\\Widget\\Mymodule\" text_field=\"Hello World\" dropdown_field=\"1\" yes_no_field=\"1\"}}<\/pre>\n<p>At final, Clear the cache and check for the result.<\/p>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to create custom widget in magento 2. First we will create the widget configuration file widget.xml, that contains all widget fields. So, create the widget.xml file with following content. File Path: etc\/widget.xml &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt; &lt;widgets xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:module:Magento_Widget:etc\/widget.xsd&#8221;&gt; &lt;widget id=&#8221;my_module_widget&#8221; class=&#8221;Ht\\Mymodule\\Block\\Widget\\Mymodule&#8221;&gt; &lt;label translate=&#8221;true&#8221;&gt;Ht Mymodule Widget&lt;\/label&gt; &lt;description&gt;Ht&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-create-custom-widget\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7335,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59],"tags":[685,10,686,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7265"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7265"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7265\/revisions"}],"predecessor-version":[{"id":7271,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7265\/revisions\/7271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7335"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}