Skip to content

Commit 8568503

Browse files
author
Valeriy Naida
authored
ENGCOM-2990: Added HTML renderer for description and short description #140
2 parents 2eebc49 + b3b44af commit 8568503

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Catalog\Helper\Output as OutputHelper;
16+
17+
/**
18+
* Resolve rendered content for attributes where HTML content is allowed
19+
*/
20+
class ProductHtmlAttribute implements ResolverInterface
21+
{
22+
/**
23+
* @var OutputHelper
24+
*/
25+
private $outputHelper;
26+
27+
/**
28+
* @param OutputHelper $outputHelper
29+
*/
30+
public function __construct(
31+
OutputHelper $outputHelper
32+
) {
33+
$this->outputHelper = $outputHelper;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function resolve(
40+
Field $field,
41+
$context,
42+
ResolveInfo $info,
43+
array $value = null,
44+
array $args = null
45+
) {
46+
if (!isset($value['model'])) {
47+
throw new GraphQlInputException(__('"model" value should be specified'));
48+
}
49+
50+
/* @var $product Product */
51+
$product = $value['model'];
52+
$fieldName = $field->getName();
53+
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
54+
return $renderedValue;
55+
}
56+
}

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
248248
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
249249
name: String @doc(description: "The product name. Customers use this name to identify the product.")
250250
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
251-
description: String @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
252-
short_description: String @doc(description: "A short description of the product. Its use depends on the theme.")
251+
description: String @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
252+
short_description: String @doc(description: "A short description of the product. Its use depends on the theme.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
253253
special_price: Float @doc(description: "The discounted price of the product")
254254
special_from_date: String @doc(description: "The beginning date that a product has a special price")
255255
special_to_date: String @doc(description: "The end date that a product has a special price")
@@ -548,6 +548,6 @@ type SortField {
548548
}
549549

550550
type SortFields @doc(description: "SortFields contains a default value for sort fields and all available sort fields") {
551-
default: String @doc(description: "Default value of sort fields")
551+
default: String @doc(description: "Default value of sort fields")
552552
options: [SortField] @doc(description: "Available sort fields")
553553
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Catalog;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\TestFramework\ObjectManager;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test for checking that product fields with directives allowed are rendered correctly
17+
*/
18+
class ProductWithDescriptionDirectivesTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var \Magento\TestFramework\ObjectManager
22+
*/
23+
private $objectManager;
24+
25+
protected function setUp()
26+
{
27+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
28+
}
29+
30+
/**
31+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
32+
* @magentoApiDataFixture Magento/Cms/_files/block.php
33+
*/
34+
public function testHtmlDirectivesRendered()
35+
{
36+
$productSku = 'simple';
37+
$cmsBlockId = 'fixture_block';
38+
$assertionCmsBlockText = 'Fixture Block Title';
39+
40+
/** @var ProductRepositoryInterface $productRepository */
41+
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
42+
/** @var ProductInterface $product */
43+
$product = $productRepository->get($productSku, false, null, true);
44+
$product->setDescription('Test: {{block id="' . $cmsBlockId . '"}}');
45+
$product->setShortDescription('Test: {{block id="' . $cmsBlockId . '"}}');
46+
$productRepository->save($product);
47+
48+
$query = <<<QUERY
49+
{
50+
products(filter: {sku: {eq: "{$productSku}"}}) {
51+
items {
52+
description
53+
short_description
54+
}
55+
}
56+
}
57+
QUERY;
58+
$response = $this->graphQlQuery($query);
59+
60+
self::assertContains($assertionCmsBlockText, $response['products']['items'][0]['description']);
61+
self::assertNotContains('{{block id', $response['products']['items'][0]['description']);
62+
self::assertContains($assertionCmsBlockText, $response['products']['items'][0]['short_description']);
63+
self::assertNotContains('{{block id', $response['products']['items'][0]['short_description']);
64+
}
65+
}

0 commit comments

Comments
 (0)