diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php index e55db2a3fa42..6e508d7e4ad9 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -14,6 +14,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Store\Api\Data\StoreInterface; /** * CMS blocks field resolver, used for GraphQL request processing @@ -46,7 +47,8 @@ public function resolve( ) { $blockIdentifiers = $this->getBlockIdentifiers($args); - $blocksData = $this->getBlocksData($blockIdentifiers); + $currentStore = $context->getExtensionAttributes()->getStore(); + $blocksData = $this->getBlocksData($blockIdentifiers, $currentStore); $resultData = [ 'items' => $blocksData, @@ -74,15 +76,15 @@ private function getBlockIdentifiers(array $args): array * Get blocks data * * @param array $blockIdentifiers + * @param StoreInterface $currentStore * @return array - * @throws GraphQlNoSuchEntityException */ - private function getBlocksData(array $blockIdentifiers): array + private function getBlocksData(array $blockIdentifiers, StoreInterface $currentStore): array { $blocksData = []; foreach ($blockIdentifiers as $blockIdentifier) { try { - $blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier); + $blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier, $currentStore); } catch (NoSuchEntityException $e) { $blocksData[$blockIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index fa4944381b85..4c126bd2a3f2 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -7,9 +7,12 @@ namespace Magento\CmsGraphQl\Model\Resolver\DataProvider; -use Magento\Cms\Api\BlockRepositoryInterface; use Magento\Cms\Api\Data\BlockInterface; +use Magento\Cms\Model\Block as BlockModel; +use Magento\Cms\Model\ResourceModel\Block\Collection as BlockCollection; +use Magento\Cms\Model\ResourceModel\Block\CollectionFactory as BlockCollectionFactory; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Store\Api\Data\StoreInterface; use Magento\Widget\Model\Template\FilterEmulate; /** @@ -18,38 +21,50 @@ class Block { /** - * @var BlockRepositoryInterface + * @var FilterEmulate */ - private $blockRepository; + private $widgetFilter; /** - * @var FilterEmulate + * @var BlockCollectionFactory */ - private $widgetFilter; + private $blockCollectionFactory; /** - * @param BlockRepositoryInterface $blockRepository + * @param BlockCollectionFactory $blockCollectionFactory * @param FilterEmulate $widgetFilter */ public function __construct( - BlockRepositoryInterface $blockRepository, + BlockCollectionFactory $blockCollectionFactory, FilterEmulate $widgetFilter ) { - $this->blockRepository = $blockRepository; $this->widgetFilter = $widgetFilter; + $this->blockCollectionFactory = $blockCollectionFactory; } /** * Get block data * * @param string $blockIdentifier + * @param StoreInterface $currentStore * @return array * @throws NoSuchEntityException */ - public function getData(string $blockIdentifier): array + public function getData(string $blockIdentifier, StoreInterface $currentStore): array { - $block = $this->blockRepository->getById($blockIdentifier); + $filterBy = BlockInterface::IDENTIFIER; + $storeId = (int)$currentStore->getId(); + if (is_numeric($blockIdentifier)) { + $filterBy = BlockInterface::BLOCK_ID; + } + + /** @var BlockCollection $collection */ + $collection = $this->blockCollectionFactory->create(); + $collection->addFieldToFilter($filterBy, ["eq" => $blockIdentifier]); + $collection->addFieldToFilter("store_id", ["eq" => $storeId]); + /** @var BlockModel $block */ + $block = $collection->getFirstItem(); if (false === $block->isActive()) { throw new NoSuchEntityException( __('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier) diff --git a/app/code/Magento/CmsGraphQl/composer.json b/app/code/Magento/CmsGraphQl/composer.json index 45255edec0cb..e9c4e89ed8a1 100644 --- a/app/code/Magento/CmsGraphQl/composer.json +++ b/app/code/Magento/CmsGraphQl/composer.json @@ -5,6 +5,7 @@ "require": { "php": "~7.1.3||~7.2.0||~7.3.0", "magento/framework": "*", + "magento/module-store": "*", "magento/module-cms": "*", "magento/module-widget": "*" }, diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php index d598a463a48a..b2cb26e9e5be 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php @@ -203,4 +203,32 @@ public function testGetEnabledAndDisabledCmsBlockInOneRequest() $responseData['errors'][0]['message'] ); } + + /** + * Verify the message when CMS Block exists but not available for a store view. + * + * @magentoApiDataFixture Magento/Cms/_files/blocks.php + * @magentoApiDataFixture Magento/Store/_files/second_store.php + * @expectedException \Exception + * @expectedExceptionMessage The CMS block with the "enabled_block" ID doesn't exist. + */ + public function testGetCmsBlockByIdentifierWithDifferentStoreView() + { + $query = + << $nonExistingStoreCode]; + $this->graphQlQuery($query, [], '', $headerMapInvalidStoreCode); + } }