Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #179 from afirlejczyk/bugfix/set-store-in-cms-rebuild
Browse files Browse the repository at this point in the history
Emulate store when rebuilding cms block and pages #77.
  • Loading branch information
afirlejczyk authored Jan 9, 2020
2 parents f9e8da6 + 8c3701b commit a5f8dc0
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 71 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Fixed
- Emulate store when rebuilding cms block and pages.


## [1.8.3] (2019.12.19)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace Divante\VsbridgeIndexerCatalog\Model\Indexer;

use Magento\Framework\Indexer\{Config\DependencyInfoProviderInterface, IndexerRegistry};
use Magento\Framework\Indexer\Config\DependencyInfoProviderInterface;
use Magento\Framework\Indexer\IndexerRegistry;

/**
* Class ProductProcessor
Expand Down
35 changes: 2 additions & 33 deletions src/module-vsbridge-indexer-cms/Model/Indexer/Action/CmsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

namespace Divante\VsbridgeIndexerCms\Model\Indexer\Action;

use Divante\VsbridgeIndexerCms\Api\ContentProcessorInterface;
use Divante\VsbridgeIndexerCms\Model\ResourceModel\CmsBlock as CmsBlockResource;

use Magento\Cms\Model\Template\FilterProvider;
use Magento\Framework\App\Area;
use Magento\Framework\App\AreaList;

/**
* Class CmsBlock
*/
Expand All @@ -25,39 +20,15 @@ class CmsBlock
*/
private $resourceModel;

/**
* @var \Magento\Cms\Model\Template\FilterProvider
*/
private $filterProvider;

/**
* @var AreaList
*/
private $areaList;

/**
* @var ContentProcessorInterface
*/
private $contentProcessor;

/**
* CmsBlock constructor.
*
* @param AreaList $areaList
* @param ContentProcessorInterface $contentProcessor
* @param CmsBlockResource $cmsBlockResource
* @param FilterProvider $filterProvider
*/
public function __construct(
AreaList $areaList,
ContentProcessorInterface $contentProcessor,
CmsBlockResource $cmsBlockResource,
FilterProvider $filterProvider
CmsBlockResource $cmsBlockResource
) {
$this->areaList = $areaList;
$this->filterProvider = $filterProvider;
$this->resourceModel = $cmsBlockResource;
$this->contentProcessor = $contentProcessor;
}

/**
Expand All @@ -68,8 +39,6 @@ public function __construct(
*/
public function rebuild($storeId = 1, array $blockIds = [])
{
$this->areaList->getArea(Area::AREA_FRONTEND)->load(Area::PART_DESIGN);
$templateFilter = $this->filterProvider->getBlockFilter()->setStoreId($storeId);
$lastBlockId = 0;

do {
Expand All @@ -78,7 +47,7 @@ public function rebuild($storeId = 1, array $blockIds = [])
foreach ($cmsBlocks as $blockData) {
$lastBlockId = (int)$blockData['block_id'];
$blockData['id'] = $lastBlockId;
$blockData['content'] = $this->contentProcessor->parse($templateFilter, (string) $blockData['content']);
$blockData['content'] = (string) $blockData['content'];
$blockData['active'] = (bool)$blockData['is_active'];

unset($blockData['creation_time'], $blockData['update_time'], $blockData['block_id']);
Expand Down
38 changes: 3 additions & 35 deletions src/module-vsbridge-indexer-cms/Model/Indexer/Action/CmsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

namespace Divante\VsbridgeIndexerCms\Model\Indexer\Action;

use Divante\VsbridgeIndexerCms\Api\ContentProcessorInterface;
use Divante\VsbridgeIndexerCms\Model\ResourceModel\CmsPage as CmsPageResource;

use Magento\Cms\Model\Template\FilterProvider;
use Magento\Framework\App\Area;
use Magento\Framework\App\AreaList;

/**
* Class CmsPage
*/
Expand All @@ -25,39 +20,14 @@ class CmsPage
*/
private $resourceModel;

/**
* @var \Magento\Cms\Model\Template\FilterProvider
*/
private $filterProvider;

/**
* @var AreaList
*/
private $areaList;

/**
* @var ContentProcessorInterface
*/
private $contentProcessor;

/**
* CmsBlock constructor.
*
* @param AreaList $areaList
* @param ContentProcessorInterface $contentProcessor
* @param CmsPageResource $cmsBlockResource
* @param FilterProvider $filterProvider
*/
public function __construct(
AreaList $areaList,
ContentProcessorInterface $contentProcessor,
CmsPageResource $cmsBlockResource,
FilterProvider $filterProvider
) {
$this->areaList = $areaList;
public function __construct(CmsPageResource $cmsBlockResource)
{
$this->resourceModel = $cmsBlockResource;
$this->filterProvider = $filterProvider;
$this->contentProcessor = $contentProcessor;
}

/**
Expand All @@ -68,8 +38,6 @@ public function __construct(
*/
public function rebuild($storeId = 1, array $pageIds = [])
{
$this->areaList->getArea(Area::AREA_FRONTEND)->load(Area::PART_DESIGN);
$templateFilter = $this->filterProvider->getPageFilter()->setStoreId($storeId);
$lastPageId = 0;

do {
Expand All @@ -78,7 +46,7 @@ public function rebuild($storeId = 1, array $pageIds = [])
foreach ($cmsPages as $pageData) {
$lastPageId = (int)$pageData['page_id'];
$pageData['id'] = $lastPageId;
$pageData['content'] = $this->contentProcessor->parse($templateFilter, (string) $pageData['content']);
$pageData['content'] = $pageData['content'];
$pageData['active'] = (bool)$pageData['is_active'];

if (isset($pageData['sort_order'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);
/**
* @package Divante\VsbridgeIndexerCms
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\Block;

use Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\CmsContentFilter;
use Divante\VsbridgeIndexerCore\Api\DataProviderInterface;

/**
* Class ContentData
*/
class ContentData implements DataProviderInterface
{
/**
* @var CmsContentFilter
*/
private $cmsContentFilter;

/**
* ContentData constructor.
*
* @param CmsContentFilter $cmsContentFilter
*/
public function __construct(CmsContentFilter $cmsContentFilter)
{
$this->cmsContentFilter = $cmsContentFilter;
}

/**
* @inheritdoc
*/
public function addData(array $indexData, $storeId)
{
return $this->cmsContentFilter->filter($indexData, $storeId, 'block');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php declare(strict_types = 1);
/**
* @package Divante\VsbridgeIndexerCms
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider;

use Divante\VsbridgeIndexerCms\Api\ContentProcessorInterface;
use Magento\Cms\Model\Template\FilterProvider;
use Magento\Framework\App\Area;
use Magento\Framework\App\AreaList;
use Magento\Store\Model\App\Emulation;

/**
* Class AbstractContentData
*/
class CmsContentFilter
{
/**
* @var \Magento\Cms\Model\Template\FilterProvider
*/
private $filterProvider;

/**
* @var AreaList
*/
private $areaList;

/**
* @var Emulation
*/
private $appEmulation;

/**
* @var ContentProcessorInterface
*/
private $contentProcessor;

/**
* CmsBlock constructor.
*
* @param AreaList $areaList
* @param Emulation $emulation
* @param ContentProcessorInterface $contentProcessor
* @param FilterProvider $filterProvider
*/
public function __construct(
AreaList $areaList,
Emulation $emulation,
ContentProcessorInterface $contentProcessor,
FilterProvider $filterProvider
) {
$this->areaList = $areaList;
$this->appEmulation = $emulation;
$this->filterProvider = $filterProvider;
$this->contentProcessor = $contentProcessor;
}

/**
* @param array $indexData
* @param int $storeId
* @param string $type
*
* @return array
* @throws Exception
*/
public function filter(array $indexData, int $storeId, string $type)
{
$this->appEmulation->startEnvironmentEmulation($storeId);
$this->areaList->getArea(Area::AREA_FRONTEND)->load(Area::PART_DESIGN);
$templateFilter = $this->geTemplateFilter($type)->setStoreId($storeId);

foreach ($indexData as &$cms) {
$cms['content'] = $this->contentProcessor->parse($templateFilter, $cms['content']);
}

$this->appEmulation->stopEnvironmentEmulation();

return $indexData;
}

/**
* @param string $type
*
* @return \Magento\Framework\Filter\Template
*/
private function geTemplateFilter(string $type): \Magento\Framework\Filter\Template
{
return $type === 'block' ? $this->filterProvider->getBlockFilter() : $this->filterProvider->getPageFilter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);
/**
* @package Divante\VsbridgeIndexerCms
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\Page;

use Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\CmsContentFilter;
use Divante\VsbridgeIndexerCore\Api\DataProviderInterface;

/**
* Class ContentData
*/
class ContentData implements DataProviderInterface
{
/**
* @var CmsContentFilter
*/
private $cmsContentFilter;

/**
* ContentData constructor.
*
* @param CmsContentFilter $cmsContentFilter
*/
public function __construct(CmsContentFilter $cmsContentFilter)
{
$this->cmsContentFilter = $cmsContentFilter;
}

/**
* @inheritdoc
*/
public function addData(array $indexData, $storeId)
{
return $this->cmsContentFilter->filter($indexData, $storeId, 'page');
}
}
12 changes: 10 additions & 2 deletions src/module-vsbridge-indexer-cms/etc/vsbridge_indices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
xsi:noNamespaceSchemaLocation="urn:magento:module:Divante_VsbridgeIndexerCore:etc/vsbridge_indices.xsd">

<index identifier="vue_storefront_catalog">
<type name="cms_block" mapping="Divante\VsbridgeIndexerCms\Index\Mapping\CmsBlock" />
<type name="cms_page" mapping="Divante\VsbridgeIndexerCms\Index\Mapping\CmsPage" />
<type name="cms_block" mapping="Divante\VsbridgeIndexerCms\Index\Mapping\CmsBlock">
<data_providers>
<data_provider name="content">Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\Block\ContentData</data_provider>
</data_providers>
</type>
<type name="cms_page" mapping="Divante\VsbridgeIndexerCms\Index\Mapping\CmsPage">
<data_providers>
<data_provider name="content">Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\Page\ContentData</data_provider>
</data_providers>
</type>
</index>
</indices>

0 comments on commit a5f8dc0

Please sign in to comment.