/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing Widgets:'); $pageGroupConfig = ['pages' => ['block' => '', 'for' => 'all', 'layout_handle' => 'default', 'template' => 'widget/static_block/default.phtml', 'page_id' => ''], 'all_pages' => ['block' => '', 'for' => 'all', 'layout_handle' => 'default', 'template' => 'widget/static_block/default.phtml', 'page_id' => ''], 'anchor_categories' => ['entities' => '', 'block' => '', 'for' => 'all', 'is_anchor_only' => 0, 'layout_handle' => 'catalog_category_view_type_layered', 'template' => 'widget/static_block/default.phtml', 'page_id' => '']]; foreach ($this->fixtures as $file) { $fileName = $this->fixtureHelper->getPath($file); $csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']); foreach ($csvReader as $row) { $block = $this->cmsBlockFactory->create()->load($row['block_identifier'], 'identifier'); if (!$block) { continue; } $widgetInstance = $this->widgetFactory->create(); $code = $row['type_code']; $themeId = $this->themeCollectionFactory->create()->getThemeByFullPath($row['theme_path'])->getId(); $type = $widgetInstance->getWidgetReference('code', $code, 'type'); $pageGroup = []; $group = $row['page_group']; $pageGroup['page_group'] = $group; $pageGroup[$group] = array_merge($pageGroupConfig[$group], unserialize($row['group_data'])); if (!empty($pageGroup[$group]['entities'])) { $pageGroup[$group]['entities'] = $this->getCategoryByUrlKey($pageGroup[$group]['entities'])->getId(); } $widgetInstance->setType($type)->setCode($code)->setThemeId($themeId); $widgetInstance->setTitle($row['title'])->setStoreIds([\Magento\Store\Model\Store::DEFAULT_STORE_ID])->setWidgetParameters(['block_id' => $block->getId()])->setPageGroups([$pageGroup]); $widgetInstance->save(); $this->logger->logInline('.'); } } }
/** * Init widget instance object and set it to registry * * @return \Magento\Widget\Model\Widget\Instance|boolean */ protected function _initWidgetInstance() { /** @var $widgetInstance \Magento\Widget\Model\Widget\Instance */ $widgetInstance = $this->_widgetFactory->create(); $code = $this->getRequest()->getParam('code', null); $instanceId = $this->getRequest()->getParam('instance_id', null); if ($instanceId) { $widgetInstance->load($instanceId)->setCode($code); if (!$widgetInstance->getId()) { $this->messageManager->addError(__('Please specify a correct widget.')); return false; } } else { // Widget id was not provided on the query-string. Locate the widget instance // type (namespace\classname) based upon the widget code (aka, widget id). $themeId = $this->getRequest()->getParam('theme_id', null); $type = $code != null ? $widgetInstance->getWidgetReference('code', $code, 'type') : null; $widgetInstance->setType($type)->setCode($code)->setThemeId($themeId); } $this->_coreRegistry->register('current_widget_instance', $widgetInstance); return $widgetInstance; }
/** * {@inheritdoc} */ public function install(array $fixtures) { $pageGroupConfig = ['pages' => ['block' => '', 'for' => 'all', 'layout_handle' => 'default', 'template' => 'widget/static_block/default.phtml', 'page_id' => ''], 'all_pages' => ['block' => '', 'for' => 'all', 'layout_handle' => 'default', 'template' => 'widget/static_block/default.phtml', 'page_id' => ''], 'anchor_categories' => ['entities' => '', 'block' => '', 'for' => 'all', 'is_anchor_only' => 0, 'layout_handle' => 'catalog_category_view_type_layered', 'template' => 'widget/static_block/default.phtml', 'page_id' => '']]; foreach ($fixtures as $fileName) { $fileName = $this->fixtureManager->getFixture($fileName); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $row = $data; /** @var \Magento\Widget\Model\ResourceModel\Widget\Instance\Collection $instanceCollection */ $instanceCollection = $this->appCollectionFactory->create(); $instanceCollection->addFilter('title', $row['title']); if ($instanceCollection->count() > 0) { continue; } /** @var \Magento\Cms\Model\Block $block */ $block = $this->cmsBlockFactory->create()->load($row['block_identifier'], 'identifier'); if (!$block) { continue; } $widgetInstance = $this->widgetFactory->create(); $code = $row['type_code']; $themeId = $this->themeCollectionFactory->create()->getThemeByFullPath($row['theme_path'])->getId(); $type = $widgetInstance->getWidgetReference('code', $code, 'type'); $pageGroup = []; $group = $row['page_group']; $pageGroup['page_group'] = $group; $pageGroup[$group] = array_merge($pageGroupConfig[$group], unserialize($row['group_data'])); if (!empty($pageGroup[$group]['entities'])) { $pageGroup[$group]['entities'] = $this->getCategoryByUrlKey($pageGroup[$group]['entities'])->getId(); } $widgetInstance->setType($type)->setCode($code)->setThemeId($themeId); $widgetInstance->setTitle($row['title'])->setStoreIds([\Magento\Store\Model\Store::DEFAULT_STORE_ID])->setWidgetParameters(['block_id' => $block->getId()])->setPageGroups([$pageGroup]); $widgetInstance->save(); } } }