Skip to content

Commit

Permalink
Render category description directives
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Aug 5, 2018
1 parent a5107ab commit 83dc64e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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,73 @@
<?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\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
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 ValueFactory
*/
private $valueFactory;

/**
* @var OutputHelper
*/
private $outputHelper;

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

/**
* {@inheritdoc}
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
if (!isset($value['model'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
}

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

$result = function () use ($renderedValue) {
return $renderedValue;
};

return $this->valueFactory->create($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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 Down
4 changes: 2 additions & 2 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,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 Expand Up @@ -548,6 +548,6 @@ type SortField {
}

type SortFields @doc(description: "SortFields contains a default value for sort fields and all available sort fields") {
default: String @doc(description: "Default value of sort fields")
default: String @doc(description: "Default value of sort fields")
options: [SortField] @doc(description: "Available sort fields")
}

0 comments on commit 83dc64e

Please sign in to comment.