1

I have a custom module for megamenu. I want to create some widgets predefined for the module when the customer installs this module. I have added the below script for creating the widget(widget type: cms block)

This is my InstallData.php

namespace Vendor\MegaMenu\Setup;
use Magento\Framework\Setup;

class InstallData implements Setup\InstallDataInterface
{
    /**
     * @var Setup\SampleData\Executor
     */
    protected $executor;
    /**
     * @var Installer
     */
    protected $installer;

    private $widgetFactory;

    public function __construct(
        Setup\SampleData\Executor $executor, 
        Installer $installer,
        \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory
        )
    {
        $this->executor = $executor;
        $this->installer = $installer;
        $this->widgetFactory = $widgetFactory;
    }

    public function install(Setup\ModuleDataSetupInterface $setup, Setup\ModuleContextInterface $moduleContext)
    {
        $this->executor->exec($this->installer);

        $megaMenuWidget = [
            'instance_type' => 'Magento\Cms\Block\Widget\Block',
            'theme_id' => 2,
            'title' => 'Test Megamenu Horizontal',
            'store_ids' => '0',
            'widget_parameters' => 'a:1:{s:8:"block_id";s:1:"11";}',
            'sort_order' => 1
        ];

        $this->widgetFactory->create()->setData($megaMenuWidget)->save();

    }

}

The widget is created. But not able to set layout update field and assign static block. Please refer the screenshot

http://i.prntscr.com/NEp1Nu_wToOwDCJYlQR_-g.png

How can I create a widget using upgrade script? My Magento version is 2.1.x

Please help me with a solution...

3

This worked for me please try it.

$megaMenuWidget = [
    'instance_type' => 'Magento\Cms\Block\Widget\Block',
    'theme_id' => 2,
    'title' => 'Test Megamenu Horizontal',
    'store_ids' => '0',
    'widget_parameters' => 'a:1:{s:8:"block_id";s:1:"11";}',
    'sort_order' => 1,
    'page_groups' => [[
        'page_group' => 'all_pages',
        'all_pages' => [
            'page_id' => null,
            //'group' => 'all_pages',
            'layout_handle' => 'default',
            'block' => 'top.container',
            'for' => 'all',
            'template' => 'widget/static_block/default.phtml'
        ]
    ]]
];
2
0

This will work for catalog product list type widgets:

$catalogProductListWidget = [
        'instance_type' => 'Vendor\Module\Block\Widget\Product\CustomProductList', // widget.xml block class
        'instance_code' => 'custom_product_items_widget', //widget.xml id
        'theme_id' => 7, //admin theme edit id
        'title' => 'CUSTOM ITEMS AVAILABLE',
        'store_ids' => '2,5', //admin store edit id
        'widget_parameters' => '{"title":"CUSTOM ITEMS AVAILABLE",'.
            '"show_buttons":["add_to_cart","add_to_wishlist"],"cache_lifetime":"",'.
            '"conditions":{"1":{"type":"Magento\\\CatalogWidget\\\Model\\\Rule\\\Condition\\\Combine",'.
            '"aggregator":"all","value":"1","new_child":""}}}',
        'sort_order' => 1,
        'page_groups' => [[
            'page_id' => 1,
            'page_group' => 'all_products',
            'layout_handle' => 'catalog_product_view',
            'for' => 'all',
            'all_products' => [
                'page_id' => null,
                'layout_handle' => 'catalog_product_view',
                'block' => 'product.info.extrahint', // widget.xml containers
                'for' => 'all',
                'template' => 'Vendor_Module::product/widget/content/grid.phtml' // widget template phtml
            ]
        ]]
    ];

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.