/** * {@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('.'); } } }
/** * @param array $data * @return \Magento\Cms\Model\Block */ protected function saveCmsBlock($data) { $cmsBlock = $this->blockFactory->create(); $cmsBlock->getResource()->load($cmsBlock, $data['identifier']); if (!$cmsBlock->getData()) { $cmsBlock->setData($data); } else { $cmsBlock->addData($data); } $cmsBlock->setStores([\Magento\Store\Model\Store::DEFAULT_STORE_ID]); $cmsBlock->setIsActive(1); $cmsBlock->save(); return $cmsBlock; }
/** * Prepare chooser element HTML * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element * @return \Magento\Framework\Data\Form\Element\AbstractElement */ public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $uniqId = $this->mathRandom->getUniqueHash($element->getId()); $sourceUrl = $this->getUrl('cms/block_widget/chooser', ['uniq_id' => $uniqId]); $chooser = $this->getLayout()->createBlock('Magento\\Widget\\Block\\Adminhtml\\Widget\\Chooser')->setElement($element)->setConfig($this->getConfig())->setFieldsetId($this->getFieldsetId())->setSourceUrl($sourceUrl)->setUniqId($uniqId); if ($element->getValue()) { $block = $this->_blockFactory->create()->load($element->getValue()); if ($block->getId()) { $chooser->setLabel($this->escapeHtml($block->getTitle())); } } $element->setData('after_element_html', $chooser->toHtml()); return $element; }
/** * Prepare Content HTML * * @return string */ protected function _toHtml() { $blockId = $this->getBlockId(); $html = ''; if ($blockId) { $storeId = $this->_storeManager->getStore()->getId(); /** @var \Magento\Cms\Model\Block $block */ $block = $this->_blockFactory->create(); $block->setStoreId($storeId)->load($blockId); if ($block->isActive()) { $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent()); } } return $html; }
/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing CMS blocks:'); if (!$this->deployHelper->isMediaPresent()) { $this->logger->log('Sample Data Media was not installed. Skipping CMS blocks installation'); return; } foreach ($this->fixtures as $file) { /** @var \Magento\SampleData\Helper\Csv\Reader */ $fileName = $this->fixtureHelper->getPath($file); $csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']); foreach ($csvReader as $row) { $data = $this->converter->convertRow($row); $data = $data['block']; /** @var \Magento\Cms\Model\Block $cmsBlock */ $cmsBlock = $this->blockFactory->create(); $cmsBlock->getResource()->load($cmsBlock, $data['identifier']); if ($cmsBlock->getId()) { continue; } $cmsBlock->addData($data); $cmsBlock->setStores([\Magento\Store\Model\Store::DEFAULT_STORE_ID]); $cmsBlock->setIsActive(1); $cmsBlock->save(); $cmsBlock->unsetData(); $this->logger->logInline('.'); } } }
/** * Load Block data by given Block Identity * * @param string $blockId * @return Block * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getById($blockId) { $block = $this->blockFactory->create(); $this->resource->load($block, $blockId); if (!$block->getId()) { throw new NoSuchEntityException(__('CMS Block with id "%1" does not exist.', $blockId)); } return $block; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** * cms page sample */ $pageContent = <<<EOD test EOD; $cmsPage = ['title' => 'test', 'content_heading' => 'test', 'page_layout' => '1column', 'identifier' => 'test-cms-page', 'content' => $pageContent, 'is_active' => 1, 'stores' => [0], 'sort_order' => 0]; $this->createPage()->setData($cmsPage)->save(); /** * cms block sample */ $cmsBlock = ['title' => 'test cms block', 'identifier' => 'test_cms_block', 'content' => 'cms block sample', 'is_active' => 1, 'stores' => 0]; /** @var \Magento\Cms\Model\Block $block */ $block = $this->blockFactory->create(); $block->setData($cmsBlock)->save(); }
/** * Prepare block text and determine whether block output enabled or not * Prevent blocks recursion if needed * * @return $this */ protected function _beforeToHtml() { parent::_beforeToHtml(); $blockId = $this->getData('block_id'); $blockHash = get_class($this) . $blockId; if (isset(self::$_widgetUsageMap[$blockHash])) { return $this; } self::$_widgetUsageMap[$blockHash] = true; if ($blockId) { $storeId = $this->_storeManager->getStore()->getId(); /** @var \Magento\Cms\Model\Block $block */ $block = $this->_blockFactory->create(); $block->setStoreId($storeId)->load($blockId); if ($block->isActive()) { $this->setText($this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent())); } } unset(self::$_widgetUsageMap[$blockHash]); return $this; }
/** * {@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(); } } }
/** * Create block * * @return \Magento\Cms\Model\Block */ public function createBlock() { return $this->_blockFactory->create(); }