@@ -34,7 +34,14 @@ class Observer
34
34
*
35
35
* @var \Magento\Catalog\Model\Layer
36
36
*/
37
- protected $ _catalogLayer ;
37
+ private $ _catalogLayer = null ;
38
+
39
+ /**
40
+ * Catalog layer resolver
41
+ *
42
+ * @var \Magento\Catalog\Model\Layer\Resolver
43
+ */
44
+ protected $ layerResolver ;
38
45
39
46
/**
40
47
* Store manager
@@ -95,7 +102,7 @@ public function __construct(
95
102
$ this ->_categoryResource = $ categoryResource ;
96
103
$ this ->_catalogProduct = $ catalogProduct ;
97
104
$ this ->_storeManager = $ storeManager ;
98
- $ this ->_catalogLayer = $ layerResolver-> get () ;
105
+ $ this ->layerResolver = $ layerResolver ;
99
106
$ this ->_catalogCategory = $ catalogCategory ;
100
107
$ this ->_catalogData = $ catalogData ;
101
108
$ this ->categoryFlatConfig = $ categoryFlatState ;
@@ -142,27 +149,10 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
142
149
if (!$ category ->getIsActive ()) {
143
150
continue ;
144
151
}
145
-
146
- $ nodeId = 'category-node- ' . $ category ->getId ();
147
-
148
152
$ block ->addIdentity (\Magento \Catalog \Model \Category::CACHE_TAG . '_ ' . $ category ->getId ());
149
153
150
154
$ tree = $ parentCategoryNode ->getTree ();
151
-
152
- $ isActiveCategory = false ;
153
- /** @var \Magento\Catalog\Model\Category $currentCategory */
154
- $ currentCategory = $ this ->_registry ->registry ('current_category ' );
155
- if ($ currentCategory && $ currentCategory ->getId () == $ category ->getId ()) {
156
- $ isActiveCategory = true ;
157
- }
158
-
159
- $ categoryData = [
160
- 'name ' => $ category ->getName (),
161
- 'id ' => $ nodeId ,
162
- 'url ' => $ this ->_catalogCategory ->getCategoryUrl ($ category ),
163
- 'has_active ' => $ this ->hasActive ($ category ),
164
- 'is_active ' => $ isActiveCategory
165
- ];
155
+ $ categoryData = $ this ->getMenuCategoryData ($ category );
166
156
$ categoryNode = new \Magento \Framework \Data \Tree \Node ($ categoryData , 'id ' , $ tree , $ parentCategoryNode );
167
157
$ parentCategoryNode ->addChild ($ categoryNode );
168
158
@@ -176,6 +166,34 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
176
166
}
177
167
}
178
168
169
+ /**
170
+ * Get category data to be added to the Menu
171
+ *
172
+ * @param \Magento\Framework\Data\Tree\Node $category
173
+ * @return array
174
+ */
175
+ public function getMenuCategoryData ($ category )
176
+ {
177
+ $ nodeId = 'category-node- ' . $ category ->getId ();
178
+
179
+ $ isActiveCategory = false ;
180
+ /** @var \Magento\Catalog\Model\Category $currentCategory */
181
+ $ currentCategory = $ this ->_registry ->registry ('current_category ' );
182
+ if ($ currentCategory && $ currentCategory ->getId () == $ category ->getId ()) {
183
+ $ isActiveCategory = true ;
184
+ }
185
+
186
+ $ categoryData = [
187
+ 'name ' => $ category ->getName (),
188
+ 'id ' => $ nodeId ,
189
+ 'url ' => $ this ->_catalogCategory ->getCategoryUrl ($ category ),
190
+ 'has_active ' => $ this ->hasActive ($ category ),
191
+ 'is_active ' => $ isActiveCategory ,
192
+ ];
193
+
194
+ return $ categoryData ;
195
+ }
196
+
179
197
/**
180
198
* Checks whether category belongs to active category's path
181
199
*
@@ -184,16 +202,29 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
184
202
*/
185
203
protected function hasActive ($ category )
186
204
{
187
- if (!$ this ->_catalogLayer ) {
205
+ $ catalogLayer = $ this ->getCatalogLayer ();
206
+ if (!$ catalogLayer ) {
188
207
return false ;
189
208
}
190
209
191
- $ currentCategory = $ this -> _catalogLayer ->getCurrentCategory ();
210
+ $ currentCategory = $ catalogLayer ->getCurrentCategory ();
192
211
if (!$ currentCategory ) {
193
212
return false ;
194
213
}
195
214
196
215
$ categoryPathIds = explode (', ' , $ currentCategory ->getPathInStore ());
197
216
return in_array ($ category ->getId (), $ categoryPathIds );
198
217
}
218
+
219
+ /**
220
+ * Get catalog layer
221
+ * @return \Magento\Catalog\Model\Layer
222
+ */
223
+ private function getCatalogLayer ()
224
+ {
225
+ if ($ this ->_catalogLayer === null ) {
226
+ $ this ->_catalogLayer = $ this ->layerResolver ->get ();
227
+ }
228
+ return $ this ->_catalogLayer ;
229
+ }
199
230
}
0 commit comments