6
6
7
7
namespace Magento \CatalogGraphQl \Model \Config ;
8
8
9
+ use Magento \Catalog \Model \Product ;
10
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
11
+ use Magento \CatalogGraphQl \Model \Resolver \Products \Attributes \Collection ;
12
+ use Magento \EavGraphQl \Model \Resolver \Query \Type ;
13
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
9
14
use Magento \Framework \Config \ReaderInterface ;
10
15
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
11
16
use Magento \Framework \GraphQl \Schema \Type \Entity \MapperInterface ;
12
17
use Magento \Framework \Reflection \TypeProcessor ;
13
- use Magento \EavGraphQl \Model \Resolver \Query \Type ;
14
- use Magento \CatalogGraphQl \Model \Resolver \Products \Attributes \Collection ;
18
+ use Magento \Store \Model \ScopeInterface ;
15
19
16
20
/**
17
21
* Adds custom/eav attribute to Catalog product types in the GraphQL config.
18
22
*/
19
23
class AttributeReader implements ReaderInterface
20
24
{
25
+ public const XML_PATH_INCLUDE_DYNAMIC_ATTRIBUTES =
26
+ 'web_api/graphql/include_dynamic_attributes_as_entity_type_fields ' ;
27
+
21
28
/**
22
29
* @var MapperInterface
23
30
*/
24
- private $ mapper ;
31
+ private MapperInterface $ mapper ;
25
32
26
33
/**
27
34
* @var Type
28
35
*/
29
- private $ typeLocator ;
36
+ private Type $ typeLocator ;
30
37
31
38
/**
32
39
* @var Collection
33
40
*/
34
- private $ collection ;
41
+ private Collection $ collection ;
42
+
43
+ /**
44
+ * @var ScopeConfigInterface
45
+ */
46
+ private ScopeConfigInterface $ config ;
35
47
36
48
/**
37
49
* @param MapperInterface $mapper
38
50
* @param Type $typeLocator
39
51
* @param Collection $collection
52
+ * @param ScopeConfigInterface $config
40
53
*/
41
54
public function __construct (
42
55
MapperInterface $ mapper ,
43
56
Type $ typeLocator ,
44
- Collection $ collection
57
+ Collection $ collection ,
58
+ ScopeConfigInterface $ config
45
59
) {
46
60
$ this ->mapper = $ mapper ;
47
61
$ this ->typeLocator = $ typeLocator ;
48
62
$ this ->collection = $ collection ;
63
+ $ this ->config = $ config ;
49
64
}
50
65
51
66
/**
@@ -58,22 +73,25 @@ public function __construct(
58
73
*/
59
74
public function read ($ scope = null ) : array
60
75
{
61
- $ typeNames = $ this ->mapper ->getMappedTypes (\Magento \Catalog \Model \Product::ENTITY );
62
- $ config =[];
63
- /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
64
- foreach ($ this ->collection ->getAttributes () as $ attribute ) {
65
- $ attributeCode = $ attribute ->getAttributeCode ();
66
- $ locatedType = $ this ->typeLocator ->getType (
67
- $ attributeCode ,
68
- \Magento \Catalog \Model \Product::ENTITY
69
- ) ?: 'String ' ;
70
- $ locatedType = $ locatedType === TypeProcessor::NORMALIZED_ANY_TYPE ? 'String ' : ucfirst ($ locatedType );
71
- foreach ($ typeNames as $ typeName ) {
72
- $ config [$ typeName ]['fields ' ][$ attributeCode ] = [
73
- 'name ' => $ attributeCode ,
74
- 'type ' => $ locatedType ,
75
- 'arguments ' => []
76
- ];
76
+ $ config = [];
77
+
78
+ if ($ this ->config ->isSetFlag (self ::XML_PATH_INCLUDE_DYNAMIC_ATTRIBUTES , ScopeInterface::SCOPE_STORE )) {
79
+ $ typeNames = $ this ->mapper ->getMappedTypes (Product::ENTITY );
80
+
81
+ /** @var Attribute $attribute */
82
+ foreach ($ this ->collection ->getAttributes () as $ attribute ) {
83
+ $ attributeCode = $ attribute ->getAttributeCode ();
84
+ $ locatedType = $ this ->typeLocator ->getType ($ attributeCode , Product::ENTITY ) ?: 'String ' ;
85
+ $ locatedType = TypeProcessor::NORMALIZED_ANY_TYPE === $ locatedType ? 'String ' : ucfirst ($ locatedType );
86
+
87
+ foreach ($ typeNames as $ typeName ) {
88
+ $ config [$ typeName ]['fields ' ][$ attributeCode ] = [
89
+ 'name ' => $ attributeCode ,
90
+ 'type ' => $ locatedType ,
91
+ 'arguments ' => [],
92
+ 'deprecated ' => ['reason ' => 'Use the `custom_attributes` field instead. ' ],
93
+ ];
94
+ }
77
95
}
78
96
}
79
97
0 commit comments