|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogGraphQl\Model\Config; |
| 8 | + |
| 9 | +use Magento\Framework\Config\ReaderInterface; |
| 10 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 11 | +use Magento\Framework\GraphQl\Type\Entity\MapperInterface; |
| 12 | +use Magento\Framework\Reflection\TypeProcessor; |
| 13 | +use Magento\EavGraphQl\Model\Resolver\Query\Type; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory; |
| 15 | +use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection; |
| 16 | + |
| 17 | +/** |
| 18 | + * Adds custom/eav attribute to Catalog product types in the GraphQL config. |
| 19 | + */ |
| 20 | +class AttributeReader implements ReaderInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var MapperInterface |
| 24 | + */ |
| 25 | + private $mapper; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Type |
| 29 | + */ |
| 30 | + private $typeLocator; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CollectionFactory |
| 34 | + */ |
| 35 | + private $collectionFactory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param MapperInterface $mapper |
| 39 | + * @param Type $typeLocator |
| 40 | + * @param CollectionFactory $collectionFactory |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + MapperInterface $mapper, |
| 44 | + Type $typeLocator, |
| 45 | + CollectionFactory $collectionFactory |
| 46 | + ) { |
| 47 | + $this->mapper = $mapper; |
| 48 | + $this->typeLocator = $typeLocator; |
| 49 | + $this->collectionFactory = $collectionFactory; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Read configuration scope |
| 54 | + * |
| 55 | + * @param string|null $scope |
| 56 | + * @return array |
| 57 | + * @throws GraphQlInputException |
| 58 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 59 | + */ |
| 60 | + public function read($scope = null) |
| 61 | + { |
| 62 | + $targetStructures = $this->mapper->getMappedTypes(\Magento\Catalog\Model\Product::ENTITY); |
| 63 | + $config =[]; |
| 64 | + /** @var Collection $collection */ |
| 65 | + $collection = $this->collectionFactory->create(); |
| 66 | + $collection->addFieldToFilter('is_user_defined', '1'); |
| 67 | + $collection->addFieldToFilter('attribute_code', ['neq' => 'cost']); |
| 68 | + /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */ |
| 69 | + foreach ($collection as $attribute) { |
| 70 | + $attributeCode = $attribute->getAttributeCode(); |
| 71 | + $locatedType = $this->typeLocator->getType( |
| 72 | + $attributeCode, |
| 73 | + \Magento\Catalog\Model\Product::ENTITY |
| 74 | + ) ?: 'String'; |
| 75 | + $locatedType = $locatedType === TypeProcessor::NORMALIZED_ANY_TYPE ? 'String' : ucfirst($locatedType); |
| 76 | + foreach ($targetStructures as $structure) { |
| 77 | + $config[$structure]['fields'][$attributeCode] = [ |
| 78 | + 'name' => $attributeCode, |
| 79 | + 'type' => $locatedType, |
| 80 | + 'arguments' => [] |
| 81 | + ]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return $config; |
| 86 | + } |
| 87 | +} |
0 commit comments