Magento 2 graphql get the cms block based on store
-
06-04-2021 - |
题
I am trying to get the cms block based on store, but it is alway returning the default store block content.
The query I used :
{
cmsBlocks(identifiers: "login-data") {
items {
identifier
title
content
}
}
}
The above one is not giving block content based on the store I set in HTTP HEADER!!
I saw the cms block resolver and did not find any store-based cms block fetch
vendor/magento/module-cms-graph-ql/Model/Resolver/Blocks.php
解决方案
For this issue please apply below Patch (feel free to give your thoughts to improve the solution):
**FOR MAGENTO 2.3.3**
Index: vendor/magento/module-cms-graph-ql/Model/Resolver/DataProvider/Block.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- /vendor/magento/module-cms-graph-ql/Model/Resolver/DataProvider/Block.php (date 1579789952704)
+++ /vendor/magento/module-cms-graph-ql/Model/Resolver/DataProvider/Block.php (date 1579789952704)
@@ -11,6 +11,8 @@
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Widget\Model\Template\FilterEmulate;
+use Magento\Store\Model\StoreManagerInterface;
+use Magento\Cms\Api\GetBlockByIdentifierInterface as BlockGetter;
/**
* Cms block data provider
@@ -33,10 +35,14 @@
*/
public function __construct(
BlockRepositoryInterface $blockRepository,
- FilterEmulate $widgetFilter
+ FilterEmulate $widgetFilter,
+ StoreManagerInterface $storeManager,
+ BlockGetter $BlockGetter
) {
$this->blockRepository = $blockRepository;
$this->widgetFilter = $widgetFilter;
+ $this->storeManager = $storeManager;
+ $this->blockGetter = $BlockGetter;
}
/**
@@ -48,22 +54,25 @@
*/
public function getData(string $blockIdentifier): array
{
- $block = $this->blockRepository->getById($blockIdentifier);
-
+ /* start custom code changes to get cms based on store */
+ $storeId = (int)$this->storeManager->getStore()->getId();
+ $block = $this->blockGetter->execute($blockIdentifier, $storeId);
+ /* end custom code changes to get cms based on store */
if (false === $block->isActive()) {
throw new NoSuchEntityException(
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
);
}
-
- $renderedContent = $this->widgetFilter->filter($block->getContent());
-
- $blockData = [
- BlockInterface::BLOCK_ID => $block->getId(),
- BlockInterface::IDENTIFIER => $block->getIdentifier(),
- BlockInterface::TITLE => $block->getTitle(),
- BlockInterface::CONTENT => $renderedContent,
- ];
- return $blockData;
- }
-}
+ if($block->getIdentifier() == $blockIdentifier) {
+ $renderedContent = $this->widgetFilter->filter($block->getContent());
+ $blockData = [
+ BlockInterface::BLOCK_ID => $block->getId(),
+ BlockInterface::IDENTIFIER => $block->getIdentifier(),
+ BlockInterface::TITLE => $block->getTitle(),
+ BlockInterface::CONTENT => $renderedContent,
+ ];
+ return $blockData;
+ }
+ return '';
+ }
+}
\ No newline at end of file
FOR MAGENTO 2.3.4
Index: /vendor/magento/module-cms-graph-ql/Model/Resolver/DataProvider/Block.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- /vendor/magento/module-cms-graph-ql/Model/Resolver/DataProvider/Block.php (date 1579875025862)
+++ /vendor/magento/module-cms-graph-ql/Model/Resolver/DataProvider/Block.php (date 1579875025862)
@@ -11,6 +11,8 @@
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Widget\Model\Template\FilterEmulate;
+use Magento\Store\Model\StoreManagerInterface;
+use Magento\Cms\Api\GetBlockByIdentifierInterface as BlockGetter;
/**
* Cms block data provider
@@ -30,13 +32,19 @@
/**
* @param BlockRepositoryInterface $blockRepository
* @param FilterEmulate $widgetFilter
+ * @param StoreManagerInterface $storeManager
+ * @param BlockGetter $BlockGetter
*/
public function __construct(
BlockRepositoryInterface $blockRepository,
- FilterEmulate $widgetFilter
+ FilterEmulate $widgetFilter,
+ StoreManagerInterface $storeManager,
+ BlockGetter $BlockGetter
) {
$this->blockRepository = $blockRepository;
$this->widgetFilter = $widgetFilter;
+ $this->storeManager = $storeManager;
+ $this->blockGetter = $BlockGetter;
}
/**
@@ -48,7 +56,10 @@
*/
public function getData(string $blockIdentifier): array
{
- $block = $this->blockRepository->getById($blockIdentifier);
+ /* start custom code changes to get cms based on store */
+ $storeId = (int)$this->storeManager->getStore()->getId();
+ $block = $this->blockGetter->execute($blockIdentifier, $storeId);
+ /* end custom code changes to get cms based on store */
if (false === $block->isActive()) {
throw new NoSuchEntityException(
@@ -57,13 +68,16 @@
}
$renderedContent = $this->widgetFilter->filterDirective($block->getContent());
-
- $blockData = [
- BlockInterface::BLOCK_ID => $block->getId(),
- BlockInterface::IDENTIFIER => $block->getIdentifier(),
- BlockInterface::TITLE => $block->getTitle(),
- BlockInterface::CONTENT => $renderedContent,
- ];
- return $blockData;
+ if ($block->getIdentifier() == $blockIdentifier) {
+ $blockData = [
+ BlockInterface::BLOCK_ID => $block->getId(),
+ BlockInterface::IDENTIFIER => $block->getIdentifier(),
+ BlockInterface::TITLE => $block->getTitle(),
+ BlockInterface::CONTENT => $renderedContent,
+ ];
+ return $blockData;
+ }else{
+ return [];
+ }
}
}
其他提示
For Fixing this issue update file
Magento\CmsGraphQl\Model\Resolver\DataProvider\Block.php with below code
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CmsGraphQl\Model\Resolver\DataProvider;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Widget\Model\Template\FilterEmulate;
/**
* Cms block data provider
*/
class Block
{
/**
* @var BlockRepositoryInterface
*/
private $blockRepository;
/**
* @var FilterEmulate
*/
private $widgetFilter;
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
/**
* @param BlockRepositoryInterface $blockRepository
* @param FilterEmulate $widgetFilter
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param StoreManagerInterface $storeManager
*/
public function __construct(
BlockRepositoryInterface $blockRepository,
FilterEmulate $widgetFilter,
SearchCriteriaBuilder $searchCriteriaBuilder = null,
StoreManagerInterface $storeManager = null
) {
$this->blockRepository = $blockRepository;
$this->widgetFilter = $widgetFilter;
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: \Magento\Framework
\App\ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
$this->storeManager = $storeManager ?: \Magento\Framework
\App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
}
/**
* Get block data
*
* @param string $blockIdentifier
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $blockIdentifier): array
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('identifier', $blockIdentifier, 'eq')
->addFilter('store_id', $this->storeManager->getStore()->getId(), 'eq')
->addFilter('is_active', true, 'eq')->create();
$block = current($this->blockRepository->getList($searchCriteria)->getItems());
if (empty($block)) {
throw new NoSuchEntityException(
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
);
}
$renderedContent = $this->widgetFilter->filterDirective($block->getContent());
$blockData = [
BlockInterface::BLOCK_ID => $block->getId(),
BlockInterface::IDENTIFIER => $block->getIdentifier(),
BlockInterface::TITLE => $block->getTitle(),
BlockInterface::CONTENT => $renderedContent,
];
return $blockData;
}
}