Skip to content

Commit 0cdfdcb

Browse files
committed
Added HTML renderer for description and short description
1 parent 8c227d5 commit 0cdfdcb

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Catalog\Helper\Output as OutputHelper;
17+
18+
/**
19+
* Resolve rendered content for attributes where HTML content is allowed
20+
*/
21+
class ProductHtmlAttribute implements ResolverInterface
22+
{
23+
/**
24+
* @var ValueFactory
25+
*/
26+
private $valueFactory;
27+
28+
/**
29+
* @var OutputHelper
30+
*/
31+
private $outputHelper;
32+
33+
/**
34+
* @param ValueFactory $valueFactory
35+
* @param OutputHelper $outputHelper
36+
*/
37+
public function __construct(
38+
ValueFactory $valueFactory,
39+
OutputHelper $outputHelper
40+
) {
41+
$this->valueFactory = $valueFactory;
42+
$this->outputHelper = $outputHelper;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function resolve(
49+
Field $field,
50+
$context,
51+
ResolveInfo $info,
52+
array $value = null,
53+
array $args = null
54+
): Value {
55+
if (!isset($value['model'])) {
56+
$result = function () {
57+
return null;
58+
};
59+
return $this->valueFactory->create($result);
60+
}
61+
62+
/* @var $product Product */
63+
$product = $value['model'];
64+
$fieldName = $field->getName();
65+
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
66+
$result = function () use ($renderedValue) {
67+
return $renderedValue;
68+
};
69+
70+
return $this->valueFactory->create($result);
71+
}
72+
}

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
}

0 commit comments

Comments
 (0)