Question

I need to generate a widget block with upgradeData.php and assign a block to this widget how can I do this?

Folowing is my code for widget generation:

Setup/UpgradeData.php

public function __construct(
        \Magento\Cms\Model\BlockFactory $blockFactory,
        \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory,
        \Magento\Framework\App\State $state
    )
    {
        $this->blockFactory = $blockFactory;
        $this->widgetFactory = $widgetFactory;
        $this->state = $state;
    }

    /**
     * Upgrade data for the module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     * @throws \Exception
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '0.1.1') < 0) {
             $cmsBlockData = [
                'title' => 'Test Block',
                'identifier' => 'test-block',
                'content' => "<div class='block'>
                                <div class='block-title'>Test block</div>
                                <div class='block-content'>
                                <div class='empty'>Test block empty content.</div>
                                </div>
                                </div>",
                'is_active' => 1,
                'stores' => [0],
                'sort_order' => 0
            ];

            $cmsBlock = $this->blockFactory->create()->setData($cmsBlockData)->save();

            $widgetData = [
                'instance_type' => 'Magento\Cms\Block\Widget\Block',
                'instance_code' => 'cms_static_block',
                'theme_id' => 5,
                'title' => 'Test Widget',
                'store_ids' => '0',
                'widget_parameters' => '{"block_id":"'.$cmsBlock->getId().'"}',
                'sort_order' => 0,
                'page_groups' => [[
                    'page_id' => 1,
                    'page_group' => 'all_pages',
                    'layout_handle' => 'default',
                    'for' => 'all',
                    'all_pages' => [
                        'page_id' => null,
                        'layout_handle' => 'default',
                        'block' => 'sidebar.additional',
                        'for' => 'all',
                        'template' => 'widget/static_block/default.phtml'
                    ]
                ]]
            ];    

            $this->widgetFactory->create()->setData($widgetData)->save();    
        }
        $setup->endSetup();
    }


but this produces exception during setup:upgrade like

[Magento\Framework\Exception\LocalizedException]

Area code is not set

Was it helpful?

Solution

That error can be solved by using the following code inside your upgrade method.

$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
        $setup->startSetup();
       //...rest code
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top