문제

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...

도움이 되었습니까?

해결책

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'
        ]
    ]]
];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top