Skip to content

Commit 04c698f

Browse files
committed
MC-29929: cmsBlocks that has scope limited to specific Store View can be seen on other Store Views in case there are Blocks with identical identifiers on both Store Views
1 parent 7432155 commit 04c698f

File tree

4 files changed

+91
-36
lines changed

4 files changed

+91
-36
lines changed

app/code/Magento/CatalogCmsGraphQl/Model/Resolver/Category/Block.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
1313
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1617
use Magento\CmsGraphQl\Model\Resolver\DataProvider\Block as BlockProvider;
@@ -48,18 +49,19 @@ public function resolve(
4849
}
4950
/** @var Category $category */
5051
$category = $value['model'];
51-
$blockId = $category->getLandingPage();
52+
$blockId = (int)$category->getLandingPage();
53+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
5254

5355
if (empty($blockId)) {
5456
return null;
5557
}
5658

5759
try {
58-
$block = $this->blockProvider->getData($blockId);
60+
$blockData = $this->blockProvider->getBlockById($blockId, $storeId);
5961
} catch (NoSuchEntityException $e) {
60-
return null;
62+
return new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
6163
}
6264

63-
return $block;
65+
return $blockData;
6466
}
6567
}

app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use function is_numeric;
1718

1819
/**
1920
* CMS blocks field resolver, used for GraphQL request processing
@@ -44,14 +45,13 @@ public function resolve(
4445
array $value = null,
4546
array $args = null
4647
) {
47-
48+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
4849
$blockIdentifiers = $this->getBlockIdentifiers($args);
49-
$blocksData = $this->getBlocksData($blockIdentifiers);
50+
$blocksData = $this->getBlocksData($blockIdentifiers, $storeId);
5051

51-
$resultData = [
52+
return [
5253
'items' => $blocksData,
5354
];
54-
return $resultData;
5555
}
5656

5757
/**
@@ -74,15 +74,22 @@ private function getBlockIdentifiers(array $args): array
7474
* Get blocks data
7575
*
7676
* @param array $blockIdentifiers
77+
* @param int $storeId
7778
* @return array
7879
* @throws GraphQlNoSuchEntityException
7980
*/
80-
private function getBlocksData(array $blockIdentifiers): array
81+
private function getBlocksData(array $blockIdentifiers, int $storeId): array
8182
{
8283
$blocksData = [];
8384
foreach ($blockIdentifiers as $blockIdentifier) {
8485
try {
85-
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier);
86+
if (!is_numeric($blockIdentifier)) {
87+
$blocksData[$blockIdentifier] = $this->blockDataProvider
88+
->getBlockByIdentifier($blockIdentifier, $storeId);
89+
} else {
90+
$blocksData[$blockIdentifier] = $this->blockDataProvider
91+
->getBlockById((int)$blockIdentifier, $storeId);
92+
}
8693
} catch (NoSuchEntityException $e) {
8794
$blocksData[$blockIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
8895
}

app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Cms\Api\Data\BlockInterface;
1212
use Magento\Framework\Api\SearchCriteriaBuilder;
1313
use Magento\Framework\Exception\NoSuchEntityException;
14-
use Magento\Store\Model\StoreManagerInterface;
1514
use Magento\Widget\Model\Template\FilterEmulate;
1615

1716
/**
@@ -28,66 +27,88 @@ class Block
2827
* @var FilterEmulate
2928
*/
3029
private $widgetFilter;
31-
32-
/**
33-
* @var \Magento\Framework\Api\SearchCriteriaBuilder
34-
*/
35-
protected $searchCriteriaBuilder;
3630

3731
/**
38-
* @var \Magento\Store\Model\StoreManagerInterface
32+
* @var SearchCriteriaBuilder
3933
*/
40-
private $storeManager;
34+
private $searchCriteriaBuilder;
4135

4236
/**
4337
* @param BlockRepositoryInterface $blockRepository
4438
* @param FilterEmulate $widgetFilter
4539
* @param SearchCriteriaBuilder $searchCriteriaBuilder
46-
* @param StoreManagerInterface $storeManager
4740
*/
4841
public function __construct(
4942
BlockRepositoryInterface $blockRepository,
5043
FilterEmulate $widgetFilter,
51-
SearchCriteriaBuilder $searchCriteriaBuilder = null,
52-
StoreManagerInterface $storeManager = null
44+
SearchCriteriaBuilder $searchCriteriaBuilder
5345
) {
5446
$this->blockRepository = $blockRepository;
5547
$this->widgetFilter = $widgetFilter;
56-
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: \Magento\Framework
57-
\App\ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
58-
$this->storeManager = $storeManager ?: \Magento\Framework
59-
\App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
48+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
6049
}
6150

6251
/**
63-
* Get block data
52+
* Get block data by identifier
6453
*
6554
* @param string $blockIdentifier
55+
* @param int $storeId
6656
* @return array
6757
* @throws NoSuchEntityException
6858
*/
69-
public function getData(string $blockIdentifier): array
59+
public function getBlockByIdentifier(string $blockIdentifier, int $storeId): array
60+
{
61+
$blockData = $this->fetchBlockData($blockIdentifier, BlockInterface::IDENTIFIER, $storeId);
62+
63+
return $blockData;
64+
}
65+
66+
/**
67+
* Get block data by block_id
68+
*
69+
* @param int $blockId
70+
* @param int $storeId
71+
* @return array
72+
* @throws NoSuchEntityException
73+
*/
74+
public function getBlockById(int $blockId, int $storeId): array
75+
{
76+
$blockData = $this->fetchBlockData($blockId, BlockInterface::BLOCK_ID, $storeId);
77+
78+
return $blockData;
79+
}
80+
81+
/**
82+
* Fetch black data by either id or identifier field
83+
*
84+
* @param mixed $identifier
85+
* @param string $field
86+
* @param int $storeId
87+
* @return array
88+
* @throws NoSuchEntityException
89+
*/
90+
private function fetchBlockData($identifier, string $field, int $storeId): array
7091
{
7192
$searchCriteria = $this->searchCriteriaBuilder
72-
->addFilter('identifier', $blockIdentifier, 'eq')
73-
->addFilter('store_id', $this->storeManager->getStore()->getId(), 'eq')
74-
->addFilter('is_active', true, 'eq')->create();
75-
$block = current($this->blockRepository->getList($searchCriteria)->getItems());
93+
->addFilter($field, $identifier)
94+
->addFilter('store_id', $storeId)
95+
->addFilter(BlockInterface::IS_ACTIVE, true)->create();
7696

77-
if (empty($block)) {
97+
$blockResults = $this->blockRepository->getList($searchCriteria)->getItems();
98+
99+
if (empty($blockResults)) {
78100
throw new NoSuchEntityException(
79-
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
101+
__('The CMS block with the "%1" ID doesn\'t exist.', $identifier)
80102
);
81103
}
82104

105+
$block = current($blockResults);
83106
$renderedContent = $this->widgetFilter->filterDirective($block->getContent());
84-
85-
$blockData = [
107+
return [
86108
BlockInterface::BLOCK_ID => $block->getId(),
87109
BlockInterface::IDENTIFIER => $block->getIdentifier(),
88110
BlockInterface::TITLE => $block->getTitle(),
89111
BlockInterface::CONTENT => $renderedContent,
90112
];
91-
return $blockData;
92113
}
93114
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCms/CategoryBlockTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,29 @@ public function testCategoryCmsBlock()
5959
$this->assertEquals($block->getIdentifier(), $actualBlock['identifier']);
6060
$this->assertEquals($renderedContent, $actualBlock['content']);
6161
}
62+
63+
/**
64+
* @magentoApiDataFixture Magento/Catalog/_files/category_tree.php
65+
*/
66+
public function testCategoryWithNoCmsBlock()
67+
{
68+
$query = <<<QUERY
69+
{
70+
category(id: 401){
71+
name
72+
cms_block{
73+
identifier
74+
title
75+
content
76+
}
77+
}
78+
}
79+
QUERY;
80+
81+
$response = $this->graphQlQuery($query);
82+
$this->assertArrayNotHasKey('errors', $response);
83+
$this->assertNotEmpty($response['category']);
84+
$this->assertArrayHasKey('cms_block', $response['category']);
85+
$this->assertEquals(null, $response['category']['cms_block']);
86+
}
6287
}

0 commit comments

Comments
 (0)