Skip to content

Commit 9277600

Browse files
ENGCOM-3070: Render category description directives #148
- Merge Pull Request magento/graphql-ce#148 from magento/graphql-ce:rendered-html-category - Merged commits: 1. 83dc64e 2. 665ec85 3. 196133d 4. 101716c 5. accac0b 6. 8e9b1eb 7. cb7a755
2 parents 8460e4e + cb7a755 commit 9277600

File tree

6 files changed

+137
-3
lines changed

6 files changed

+137
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct(
7979
}
8080

8181
/**
82-
* {@inheritdoc}
82+
* @inheritdoc
8383
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8484
*/
8585
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
@@ -114,6 +114,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
114114
$categories[$item->getId()] = $this->customAttributesFlattener
115115
->flatten($categories[$item->getId()]);
116116
$categories[$item->getId()]['product_count'] = $item->getProductCount();
117+
$categories[$item->getId()]['model'] = $item;
117118
}
118119
}
119120

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
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 category attributes where HTML content is allowed
19+
*/
20+
class CategoryHtmlAttribute 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 LocalizedException(__('"model" value should be specified'));
48+
}
49+
50+
/* @var $category Category */
51+
$category = $value['model'];
52+
$fieldName = $field->getName();
53+
$renderedValue = $this->outputHelper->categoryAttribute($category, $category->getData($fieldName), $fieldName);
54+
55+
return $renderedValue;
56+
}
57+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Image.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Product;
1111
use Magento\Catalog\Model\Product\ImageFactory;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
@@ -45,7 +46,7 @@ public function resolve(
4546
array $args = null
4647
): array {
4748
if (!isset($value['model'])) {
48-
throw new \LogicException(__('"model" value should be specified'));
49+
throw new LocalizedException(__('"model" value should be specified'));
4950
}
5051
/** @var Product $product */
5152
$product = $value['model'];

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function __construct(
8383
}
8484

8585
/**
86+
* Returns categories tree starting from parent $rootCategoryId
87+
*
8688
* @param ResolveInfo $resolveInfo
8789
* @param int $rootCategoryId
8890
* @return array
@@ -107,6 +109,8 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId) : array
107109
}
108110

109111
/**
112+
* Iterates through category tree
113+
*
110114
* @param \Iterator $iterator
111115
* @return array
112116
*/
@@ -119,6 +123,7 @@ private function processTree(\Iterator $iterator) : array
119123
$iterator->next();
120124
$nextCategory = $iterator->current();
121125
$tree[$category->getId()] = $this->hydrator->hydrateCategory($category);
126+
$tree[$category->getId()]['model'] = $category;
122127
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
123128
$tree[$category->getId()]['children'] = $this->processTree($iterator);
124129
}
@@ -128,6 +133,8 @@ private function processTree(\Iterator $iterator) : array
128133
}
129134

130135
/**
136+
* Joins EAV attributes recursively
137+
*
131138
* @param Collection $collection
132139
* @param FieldNode $fieldNode
133140
* @return void

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ interface CustomizableProductInterface @typeResolver(class: "Magento\\CatalogGra
369369

370370
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") {
371371
id: Int @doc(description: "An ID that uniquely identifies the category")
372-
description: String @doc(description: "An optional description of the category")
372+
description: String @doc(description: "An optional description of the category") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryHtmlAttribute")
373373
name: String @doc(description: "The display name of the category")
374374
path: String @doc(description: "Category Path")
375375
path_in_store: String @doc(description: "Category path in store")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\TestFramework\ObjectManager;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test for checking that category description directives are rendered correctly
18+
*/
19+
class CategoryWithDescriptionDirectivesTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var \Magento\TestFramework\ObjectManager
23+
*/
24+
private $objectManager;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function setUp()
30+
{
31+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
32+
}
33+
34+
/**
35+
* @magentoApiDataFixture Magento/Catalog/_files/category.php
36+
*/
37+
public function testHtmlDirectivesRendered()
38+
{
39+
$categoryId = 333;
40+
$mediaFilePath = '/path/to/mediafile';
41+
/** @var StoreManagerInterface $storeManager */
42+
$storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
43+
$storeBaseUrl = $storeManager->getStore()->getBaseUrl();
44+
45+
/* Remove index.php from base URL */
46+
$storeBaseUrlParts = explode('/index.php', $storeBaseUrl);
47+
$storeBaseUrl = $storeBaseUrlParts[0];
48+
49+
/** @var CategoryRepositoryInterface $categoryRepository */
50+
$categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class);
51+
/** @var CategoryInterface $category */
52+
$category = $categoryRepository->get($categoryId);
53+
$category->setDescription('Test: {{media url="' . $mediaFilePath . '"}}');
54+
$categoryRepository->save($category);
55+
56+
$query = <<<QUERY
57+
{
58+
category(id: {$categoryId}) {
59+
description
60+
}
61+
}
62+
QUERY;
63+
$response = $this->graphQlQuery($query);
64+
65+
self::assertNotContains('media url', $response['category']['description']);
66+
self::assertContains($storeBaseUrl, $response['category']['description']);
67+
}
68+
}

0 commit comments

Comments
 (0)