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

Try this. This worked for me.

$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'
        ]
    ]]
];
1
  • can u confirm the widget can able to display config products? – jafar pinjar Jul 16 '19 at 11:29

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.