18
18
use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \FieldProvider \FieldName \ResolverInterface
19
19
as FieldNameResolver ;
20
20
use Magento \Framework \Api \SearchCriteriaBuilder ;
21
+ use Magento \Catalog \Model \ResourceModel \Category \Collection ;
22
+ use Magento \Framework \App \ObjectManager ;
21
23
22
24
/**
23
25
* Provide dynamic fields for product.
@@ -27,10 +29,18 @@ class DynamicField implements FieldProviderInterface
27
29
/**
28
30
* Category list.
29
31
*
32
+ * @deprecated
30
33
* @var CategoryListInterface
31
34
*/
32
35
private $ categoryList ;
33
36
37
+ /**
38
+ * Category collection.
39
+ *
40
+ * @var Collection
41
+ */
42
+ private $ categoryCollection ;
43
+
34
44
/**
35
45
* Customer group repository.
36
46
*
@@ -73,6 +83,7 @@ class DynamicField implements FieldProviderInterface
73
83
* @param CategoryListInterface $categoryList
74
84
* @param FieldNameResolver $fieldNameResolver
75
85
* @param AttributeProvider $attributeAdapterProvider
86
+ * @param Collection|null $categoryCollection
76
87
*/
77
88
public function __construct (
78
89
FieldTypeConverterInterface $ fieldTypeConverter ,
@@ -81,7 +92,8 @@ public function __construct(
81
92
SearchCriteriaBuilder $ searchCriteriaBuilder ,
82
93
CategoryListInterface $ categoryList ,
83
94
FieldNameResolver $ fieldNameResolver ,
84
- AttributeProvider $ attributeAdapterProvider
95
+ AttributeProvider $ attributeAdapterProvider ,
96
+ Collection $ categoryCollection = null
85
97
) {
86
98
$ this ->groupRepository = $ groupRepository ;
87
99
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -90,6 +102,8 @@ public function __construct(
90
102
$ this ->categoryList = $ categoryList ;
91
103
$ this ->fieldNameResolver = $ fieldNameResolver ;
92
104
$ this ->attributeAdapterProvider = $ attributeAdapterProvider ;
105
+ $ this ->categoryCollection = $ categoryCollection ?:
106
+ ObjectManager::getInstance ()->get (Collection::class);
93
107
}
94
108
95
109
/**
@@ -98,18 +112,17 @@ public function __construct(
98
112
public function getFields (array $ context = []): array
99
113
{
100
114
$ allAttributes = [];
101
- $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
102
- $ categories = $ this ->categoryList ->getList ($ searchCriteria )->getItems ();
115
+ $ categoryIds = $ this ->categoryCollection ->getAllIds ();
103
116
$ positionAttribute = $ this ->attributeAdapterProvider ->getByAttributeCode ('position ' );
104
117
$ categoryNameAttribute = $ this ->attributeAdapterProvider ->getByAttributeCode ('category_name ' );
105
- foreach ($ categories as $ category ) {
118
+ foreach ($ categoryIds as $ categoryId ) {
106
119
$ categoryPositionKey = $ this ->fieldNameResolver ->getFieldName (
107
120
$ positionAttribute ,
108
- ['categoryId ' => $ category -> getId () ]
121
+ ['categoryId ' => $ categoryId ]
109
122
);
110
123
$ categoryNameKey = $ this ->fieldNameResolver ->getFieldName (
111
124
$ categoryNameAttribute ,
112
- ['categoryId ' => $ category -> getId () ]
125
+ ['categoryId ' => $ categoryId ]
113
126
);
114
127
$ allAttributes [$ categoryPositionKey ] = [
115
128
'type ' => $ this ->fieldTypeConverter ->convert (FieldTypeConverterInterface::INTERNAL_DATA_TYPE_STRING ),
@@ -121,6 +134,7 @@ public function getFields(array $context = []): array
121
134
];
122
135
}
123
136
137
+ $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
124
138
$ groups = $ this ->groupRepository ->getList ($ searchCriteria )->getItems ();
125
139
$ priceAttribute = $ this ->attributeAdapterProvider ->getByAttributeCode ('price ' );
126
140
foreach ($ groups as $ group ) {
0 commit comments