Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function resolve(
) {

$blockIdentifiers = $this->getBlockIdentifiers($args);
$blocksData = $this->getBlocksData($blockIdentifiers);

$scopeIdentifiers = $this->getScopeIdentifiers($args);
$blocksData = $this->getBlocksData($blockIdentifiers,$scopeIdentifiers[0]);
$resultData = [
'items' => $blocksData,
];
Expand All @@ -69,20 +69,36 @@ private function getBlockIdentifiers(array $args): array

return $args['identifiers'];
}


/**
* Get scope identifiers
*
* @param array $args
* @return string[]
* @throws GraphQlInputException
*/
private function getScopeIdentifiers(array $args): array
{
if (!isset($args['scope_code']) || !is_array($args['scope_code']) || count($args['scope_code']) === 0) {
throw new GraphQlInputException(__('Scope Code of CMS blocks should be specified'));
}
return $args['scope_code'];
}

/**
* Get blocks data
*
* @param array $blockIdentifiers
* @param string $scopeargs
* @return array
* @throws GraphQlNoSuchEntityException
*/
private function getBlocksData(array $blockIdentifiers): array
private function getBlocksData(array $blockIdentifiers,$scopeargs): array
{
$blocksData = [];
foreach ($blockIdentifiers as $blockIdentifier) {
try {
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier);
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier,$scopeargs);
} catch (NoSuchEntityException $e) {
$blocksData[$blockIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
Expand Down
55 changes: 40 additions & 15 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,54 @@
declare(strict_types=1);

namespace Magento\CmsGraphQl\Model\Resolver\DataProvider;

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Widget\Model\Template\FilterEmulate;

use Magento\Cms\Model\GetBlockByIdentifier;
/**
* Cms block data provider
*/
class Block
{
/**
* @var BlockRepositoryInterface
*/
private $blockRepository;

/**
* @var BlockRepositoryInterface
* @var GetBlockByIdentifier
*/
private $blockRepository;
private $blockIdentifier;

/**
* @var FilterEmulate
*/
private $widgetFilter;

/**
* @var \Magento\Store\Model\Store
*/
private $store;

/**
* @param BlockRepositoryInterface $blockRepository
* @param FilterEmulate $widgetFilter
* @param GetBlockByIdentifier $widgetFilter
* @param \Magento\Store\Model\Store $store
*
*/

public function __construct(
BlockRepositoryInterface $blockRepository,
FilterEmulate $widgetFilter
BlockRepositoryInterface $blockRepository,
FilterEmulate $widgetFilter,
GetBlockByIdentifier $blockIdentifier,
\Magento\Store\Model\Store $store
) {
$this->blockRepository = $blockRepository;
$this->blockRepository = $blockRepository;
$this->widgetFilter = $widgetFilter;
$this->blockIdentifier = $blockIdentifier;
$this->store = $store;
}

/**
Expand All @@ -46,20 +63,28 @@ public function __construct(
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $blockIdentifier): array
public function getData(string $blockIdentifier,string $scopeargs): array
{
$block = $this->blockRepository->getById($blockIdentifier);

if($scopeargs==null){
$block = $this->blockRepository->getById($blockIdentifier);
}else{
$store = $this->store->load($scopeargs,'code');
if(!$store->getId()){
throw new NoSuchEntityException(
__('Store Does Not Exist')
);
}
$block = $this->blockIdentifier->execute($blockIdentifier,(int)$store->getId());
}

if (false === $block->isActive()) {
throw new NoSuchEntityException(
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
);
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,
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/CmsGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Query {
id: Int @doc(description: "Id of the CMS page")
): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Page") @doc(description: "The CMS page query returns information about a CMS page") @cache(cacheTag: "cms_p", cacheIdentity: "Magento\\CmsGraphQl\\Model\\Resolver\\Page\\Identity")
cmsBlocks (
identifiers: [String] @doc(description: "Identifiers of the CMS blocks")
identifiers: [String] @doc(description: "Identifiers of the CMS blocks"),
scope_code: [String] @doc(description: "Scope Code of the CMS blocks")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case: send

query {
  storeConfig {
    id
    website_id
  }
  cmsBlocks(
    identifiers: null
    scope_code: null
  ) {
    items {
      content
    }
  }
}

and get this "message": "\"identifiers\" of CMS blocks should be specified"
send another

query {
  storeConfig {
    id
    website_id
  }
  cmsBlocks(
    identifiers: ""
    scope_code: null
  ) {
    items {
      content
    }
  }
}

and get "message": "Scope Code of CMS blocks should be specified"
then send

query {
  storeConfig {
    id
    website_id
  }
  cmsBlocks(
    identifiers: ""
    scope_code: ""
  ) {
    items {
      content
    }
  }
}

and get "The CMS block with the "" ID doesn't exist."

@naydav, why identifiers is not required field. What is the point of sending empty strings. I have seen many times that sending "" or null results in error that some field is not-nullable. Maybe the better logic is to check if the property is omitted and thn address it to all or default equivalent in REST rather than allowing empty strings?

): CmsBlocks @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Blocks") @doc(description: "The CMS block query returns information about CMS blocks") @cache(cacheTag: "cms_b", cacheIdentity: "Magento\\CmsGraphQl\\Model\\Resolver\\Block\\Identity")
}

Expand Down