|
| 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 | +} |
0 commit comments