Skip to content

Commit 83dc64e

Browse files
committed
Render category description directives
1 parent a5107ab commit 83dc64e

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Categories.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
110110
$categories[$item->getId()] = $this->customAttributesFlattener
111111
->flatten($categories[$item->getId()]);
112112
$categories[$item->getId()]['product_count'] = $item->getProductCount();
113+
$categories[$item->getId()]['model'] = $item;
113114
}
114115
}
115116

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Category;
9+
10+
use Magento\Catalog\Model\Category;
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 category attributes where HTML content is allowed
20+
*/
21+
class CategoryHtmlAttribute 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 $category Category */
63+
$category = $value['model'];
64+
$fieldName = $field->getName();
65+
$renderedValue = $this->outputHelper->categoryAttribute($category, $category->getData($fieldName), $fieldName);
66+
67+
$result = function () use ($renderedValue) {
68+
return $renderedValue;
69+
};
70+
71+
return $this->valueFactory->create($result);
72+
}
73+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ private function processTree(\Iterator $iterator) : array
119119
$iterator->next();
120120
$nextCategory = $iterator->current();
121121
$tree[$category->getId()] = $this->hydrator->hydrateCategory($category);
122+
$tree[$category->getId()]['model'] = $category;
122123
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
123124
$tree[$category->getId()]['children'] = $this->processTree($iterator);
124125
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ interface CustomizableProductInterface @typeResolver(class: "Magento\\CatalogGra
364364

365365
interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\CategoryInterfaceTypeResolver") @doc(description: "CategoryInterface contains the full set of attributes that can be returned in a category search") {
366366
id: Int @doc(description: "An ID that uniquely identifies the category")
367-
description: String @doc(description: "An optional description of the category")
367+
description: String @doc(description: "An optional description of the category") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryHtmlAttribute")
368368
name: String @doc(description: "The display name of the category")
369369
path: String @doc(description: "Category Path")
370370
path_in_store: String @doc(description: "Category path in store")
@@ -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)