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

Render category description directives #148

Merged
merged 7 commits into from
Oct 3, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
Expand Down Expand Up @@ -114,6 +114,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$categories[$item->getId()] = $this->customAttributesFlattener
->flatten($categories[$item->getId()]);
$categories[$item->getId()]['product_count'] = $item->getProductCount();
$categories[$item->getId()]['model'] = $item;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use Magento\Catalog\Model\Category;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Helper\Output as OutputHelper;

/**
* Resolve rendered content for category attributes where HTML content is allowed
*/
class CategoryHtmlAttribute implements ResolverInterface
{
/**
* @var OutputHelper
*/
private $outputHelper;

/**
* @param OutputHelper $outputHelper
*/
public function __construct(
OutputHelper $outputHelper
) {
$this->outputHelper = $outputHelper;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

/* @var $category Category */
$category = $value['model'];
$fieldName = $field->getName();
$renderedValue = $this->outputHelper->categoryAttribute($category, $category->getData($fieldName), $fieldName);

return $renderedValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\ImageFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function resolve(
array $args = null
): array {
if (!isset($value['model'])) {
throw new \LogicException(__('"model" value should be specified'));
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var Product $product */
$product = $value['model'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function __construct(
}

/**
* Returns categories tree starting from parent $rootCategoryId
*
* @param ResolveInfo $resolveInfo
* @param int $rootCategoryId
* @return array
Expand All @@ -107,6 +109,8 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId) : array
}

/**
* Iterates through category tree
*
* @param \Iterator $iterator
* @return array
*/
Expand All @@ -119,6 +123,7 @@ private function processTree(\Iterator $iterator) : array
$iterator->next();
$nextCategory = $iterator->current();
$tree[$category->getId()] = $this->hydrator->hydrateCategory($category);
$tree[$category->getId()]['model'] = $category;
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
$tree[$category->getId()]['children'] = $this->processTree($iterator);
}
Expand All @@ -128,6 +133,8 @@ private function processTree(\Iterator $iterator) : array
}

/**
* Joins EAV attributes recursively
*
* @param Collection $collection
* @param FieldNode $fieldNode
* @return void
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ interface CustomizableProductInterface @typeResolver(class: "Magento\\CatalogGra

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") {
id: Int @doc(description: "An ID that uniquely identifies the category")
description: String @doc(description: "An optional description of the category")
description: String @doc(description: "An optional description of the category") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoryHtmlAttribute")
name: String @doc(description: "The display name of the category")
path: String @doc(description: "Category Path")
path_in_store: String @doc(description: "Category path in store")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for checking that category description directives are rendered correctly
*/
class CategoryWithDescriptionDirectivesTest extends GraphQlAbstract
{
/**
* @var \Magento\TestFramework\ObjectManager
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category.php
*/
public function testHtmlDirectivesRendered()
{
$categoryId = 333;
$mediaFilePath = '/path/to/mediafile';
/** @var StoreManagerInterface $storeManager */
$storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
$storeBaseUrl = $storeManager->getStore()->getBaseUrl();

/* Remove index.php from base URL */
$storeBaseUrlParts = explode('/index.php', $storeBaseUrl);
$storeBaseUrl = $storeBaseUrlParts[0];

/** @var CategoryRepositoryInterface $categoryRepository */
$categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class);
/** @var CategoryInterface $category */
$category = $categoryRepository->get($categoryId);
$category->setDescription('Test: {{media url="' . $mediaFilePath . '"}}');
$categoryRepository->save($category);

$query = <<<QUERY
{
category(id: {$categoryId}) {
description
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertNotContains('media url', $response['category']['description']);
self::assertContains($storeBaseUrl, $response['category']['description']);
}
}