|
| 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\Api\Data\ProductAttributeInterface; |
| 11 | +use Magento\Catalog\Model\FilterProductCustomAttribute; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\CatalogGraphQl\Model\ProductDataProvider; |
| 14 | +use Magento\Eav\Api\Data\AttributeInterface; |
| 15 | +use Magento\Eav\Model\AttributeRepository; |
| 16 | +use Magento\EavGraphQl\Model\Output\Value\GetAttributeValueInterface; |
| 17 | +use Magento\EavGraphQl\Model\Resolver\AttributeFilter; |
| 18 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 19 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 20 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 21 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 22 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * |
| 26 | + * Format a product's custom attribute information to conform to GraphQL schema representation |
| 27 | + */ |
| 28 | +class ProductCustomAttributes implements ResolverInterface |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var AttributeRepository |
| 32 | + */ |
| 33 | + private AttributeRepository $attributeRepository; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var SearchCriteriaBuilder |
| 37 | + */ |
| 38 | + private SearchCriteriaBuilder $searchCriteriaBuilder; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var GetAttributeValueInterface |
| 42 | + */ |
| 43 | + private GetAttributeValueInterface $getAttributeValue; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ProductDataProvider |
| 47 | + */ |
| 48 | + private ProductDataProvider $productDataProvider; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var AttributeFilter |
| 52 | + */ |
| 53 | + private AttributeFilter $attributeFilter; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var FilterProductCustomAttribute |
| 57 | + */ |
| 58 | + private FilterProductCustomAttribute $filterCustomAttribute; |
| 59 | + |
| 60 | + /** |
| 61 | + * @param AttributeRepository $attributeRepository |
| 62 | + * @param SearchCriteriaBuilder $searchCriteriaBuilder |
| 63 | + * @param GetAttributeValueInterface $getAttributeValue |
| 64 | + * @param ProductDataProvider $productDataProvider |
| 65 | + * @param AttributeFilter $attributeFilter |
| 66 | + * @param FilterProductCustomAttribute $filterCustomAttribute |
| 67 | + */ |
| 68 | + public function __construct( |
| 69 | + AttributeRepository $attributeRepository, |
| 70 | + SearchCriteriaBuilder $searchCriteriaBuilder, |
| 71 | + GetAttributeValueInterface $getAttributeValue, |
| 72 | + ProductDataProvider $productDataProvider, |
| 73 | + AttributeFilter $attributeFilter, |
| 74 | + FilterProductCustomAttribute $filterCustomAttribute |
| 75 | + ) { |
| 76 | + $this->attributeRepository = $attributeRepository; |
| 77 | + $this->searchCriteriaBuilder = $searchCriteriaBuilder; |
| 78 | + $this->getAttributeValue = $getAttributeValue; |
| 79 | + $this->productDataProvider = $productDataProvider; |
| 80 | + $this->attributeFilter = $attributeFilter; |
| 81 | + $this->filterCustomAttribute = $filterCustomAttribute; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @inheritdoc |
| 86 | + * |
| 87 | + * @param Field $field |
| 88 | + * @param ContextInterface $context |
| 89 | + * @param ResolveInfo $info |
| 90 | + * @param array|null $value |
| 91 | + * @param array|null $args |
| 92 | + * @throws \Exception |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + public function resolve( |
| 96 | + Field $field, |
| 97 | + $context, |
| 98 | + ResolveInfo $info, |
| 99 | + array $value = null, |
| 100 | + array $args = null |
| 101 | + ) { |
| 102 | + $filterArgs = $args['filter'] ?? []; |
| 103 | + |
| 104 | + $searchCriteriaBuilder = $this->attributeFilter->execute($filterArgs, $this->searchCriteriaBuilder); |
| 105 | + |
| 106 | + $searchCriteriaBuilder = $searchCriteriaBuilder |
| 107 | + ->addFilter('is_visible', true) |
| 108 | + ->addFilter('backend_type', 'static', 'neq') |
| 109 | + ->create(); |
| 110 | + |
| 111 | + $productCustomAttributes = $this->attributeRepository->getList( |
| 112 | + ProductAttributeInterface::ENTITY_TYPE_CODE, |
| 113 | + $searchCriteriaBuilder |
| 114 | + )->getItems(); |
| 115 | + |
| 116 | + $attributeCodes = array_map( |
| 117 | + function (AttributeInterface $customAttribute) { |
| 118 | + return $customAttribute->getAttributeCode(); |
| 119 | + }, |
| 120 | + $productCustomAttributes |
| 121 | + ); |
| 122 | + |
| 123 | + $filteredAttributeCodes = $this->filterCustomAttribute->execute(array_flip($attributeCodes)); |
| 124 | + |
| 125 | + /** @var Product $product */ |
| 126 | + $product = $value['model']; |
| 127 | + $productData = $this->productDataProvider->getProductDataById((int)$product->getId()); |
| 128 | + |
| 129 | + $customAttributes = []; |
| 130 | + foreach ($filteredAttributeCodes as $attributeCode => $value) { |
| 131 | + if (!array_key_exists($attributeCode, $productData)) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + $attributeValue = $productData[$attributeCode]; |
| 135 | + if (is_array($attributeValue)) { |
| 136 | + $attributeValue = implode(',', $attributeValue); |
| 137 | + } |
| 138 | + $customAttributes[] = [ |
| 139 | + 'attribute_code' => $attributeCode, |
| 140 | + 'value' => $attributeValue |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + return array_map( |
| 145 | + function (array $customAttribute) { |
| 146 | + return $this->getAttributeValue->execute( |
| 147 | + ProductAttributeInterface::ENTITY_TYPE_CODE, |
| 148 | + $customAttribute['attribute_code'], |
| 149 | + $customAttribute['value'] |
| 150 | + ); |
| 151 | + }, |
| 152 | + $customAttributes |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments