Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Fixed #176 - Show only active CMS Blocks #218

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 32 additions & 2 deletions app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Blocks implements ResolverInterface
*/
private $blockDataProvider;

/**
* Error message
*
* @var array
*/
private $errorMessage = [];

/**
* @param BlockDataProvider $blockDataProvider
*/
Expand All @@ -50,11 +57,14 @@ public function resolve(

$resultData = [
'items' => $blocksData,
'errors' => $this->errorMessage
];
return $resultData;
}

/**
* Get block identifiers
*
* @param array $args
* @return string[]
* @throws GraphQlInputException
Expand All @@ -69,6 +79,8 @@ private function getBlockIdentifiers(array $args): array
}

/**
* Get blocks data
*
* @param array $blockIdentifiers
* @return array
* @throws GraphQlNoSuchEntityException
Expand All @@ -78,11 +90,29 @@ private function getBlocksData(array $blockIdentifiers): array
$blocksData = [];
try {
foreach ($blockIdentifiers as $blockIdentifier) {
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier);
$blockData = $this->blockDataProvider->getData($blockIdentifier);
if (!empty($blockData)) {
$blocksData[$blockIdentifier] = $blockData;
} else {
$this->setErrorMessage(sprintf('The CMS block with the "%s" ID is disabled.', $blockIdentifier));
Copy link
Contributor

Choose a reason for hiding this comment

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

\Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException seems to be suitable here. And please note that it should be translatable (use __())

}
}
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
$this->setErrorMessage($e->getMessage());
}
return $blocksData;
}

/**
* Set error message
*
* @param string $error
* @return array
*/
private function setErrorMessage(string $error): array
{
$this->errorMessage[]['message'] = $error;
return $this->errorMessage;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function __construct(
}

/**
* Get block data
*
* @param string $blockIdentifier
* @return array
* @throws NoSuchEntityException
Expand All @@ -49,7 +51,7 @@ public function getData(string $blockIdentifier): array
$block = $this->blockRepository->getById($blockIdentifier);

if (false === $block->isActive()) {
throw new NoSuchEntityException();
return [];
}

$renderedContent = $this->widgetFilter->filter($block->getContent());
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/CmsGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ type CmsPage @doc(description: "CMS page defines all CMS page information") {

type CmsBlocks @doc(description: "CMS blocks information") {
items: [CmsBlock] @doc(description: "An array of CMS blocks")
errors: [ErrorMessage] @doc(description: "An array of errors message")
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not need to introduce "errors" for specific queries. "errors" functionality is available out of the box. All you need to do is to throw \Magento\Framework\GraphQl\Exception\GraphQlInputException or another exception which implements \GraphQL\Error\ClientAware from your resolver.

}

type CmsBlock @doc(description: "CMS block defines all CMS block information") {
identifier: String @doc(description: "CMS block identifier")
title: String @doc(description: "CMS block title")
content: String @doc(description: "CMS block content")
}

type ErrorMessage @doc(description: "Error message define all error information") {
message: String @doc(description: "Error message")
}