12
12
use Magento \Framework \Data \Tree \Node ;
13
13
14
14
/**
15
- * Plugin for top menu block
15
+ * Plugin that enhances the top menu block by building and managing the category tree
16
+ * for menu rendering in a storefront.
16
17
*/
17
18
class Topmenu
18
19
{
19
20
/**
20
- * Catalog category
21
- *
22
21
* @var \Magento\Catalog\Helper\Category
23
22
*/
24
23
protected $ catalogCategory ;
@@ -33,29 +32,21 @@ class Topmenu
33
32
*/
34
33
private $ storeManager ;
35
34
36
- /**
37
- * @var \Magento\Catalog\Model\Layer\Resolver
38
- */
39
- private $ layerResolver ;
40
-
41
35
/**
42
36
* Initialize dependencies.
43
37
*
44
38
* @param \Magento\Catalog\Helper\Category $catalogCategory
45
39
* @param \Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory $categoryCollectionFactory
46
40
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
47
- * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
48
41
*/
49
42
public function __construct (
50
43
\Magento \Catalog \Helper \Category $ catalogCategory ,
51
44
\Magento \Catalog \Model \ResourceModel \Category \StateDependentCollectionFactory $ categoryCollectionFactory ,
52
- \Magento \Store \Model \StoreManagerInterface $ storeManager ,
53
- \Magento \Catalog \Model \Layer \Resolver $ layerResolver
45
+ \Magento \Store \Model \StoreManagerInterface $ storeManager
54
46
) {
55
47
$ this ->catalogCategory = $ catalogCategory ;
56
48
$ this ->collectionFactory = $ categoryCollectionFactory ;
57
49
$ this ->storeManager = $ storeManager ;
58
- $ this ->layerResolver = $ layerResolver ;
59
50
}
60
51
61
52
/**
@@ -78,7 +69,6 @@ public function beforeGetHtml(
78
69
$ storeId = $ this ->storeManager ->getStore ()->getId ();
79
70
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
80
71
$ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
81
- $ currentCategory = $ this ->getCurrentCategory ();
82
72
$ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
83
73
foreach ($ collection as $ category ) {
84
74
$ categoryParentId = $ category ->getParentId ();
@@ -97,7 +87,6 @@ public function beforeGetHtml(
97
87
$ categoryNode = new Node (
98
88
$ this ->getCategoryAsArray (
99
89
$ category ,
100
- $ currentCategory ,
101
90
$ category ->getParentId () == $ categoryParentId
102
91
),
103
92
'id ' ,
@@ -132,39 +121,20 @@ public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
132
121
}
133
122
}
134
123
135
- /**
136
- * Get current Category from catalog layer
137
- *
138
- * @return \Magento\Catalog\Model\Category
139
- */
140
- private function getCurrentCategory ()
141
- {
142
- $ catalogLayer = $ this ->layerResolver ->get ();
143
-
144
- if (!$ catalogLayer ) {
145
- return null ;
146
- }
147
-
148
- return $ catalogLayer ->getCurrentCategory ();
149
- }
150
-
151
124
/**
152
125
* Convert category to array
153
126
*
154
- * @param \Magento\Catalog\Model\Category $category
155
- * @param \Magento\Catalog\Model\Category $currentCategory
127
+ * @param Category $category
156
128
* @param bool $isParentActive
157
129
* @return array
158
130
*/
159
- private function getCategoryAsArray ($ category , $ currentCategory , $ isParentActive )
131
+ private function getCategoryAsArray ($ category , $ isParentActive ): array
160
132
{
161
133
$ categoryId = $ category ->getId ();
162
134
return [
163
135
'name ' => $ category ->getName (),
164
136
'id ' => 'category-node- ' . $ categoryId ,
165
137
'url ' => $ this ->catalogCategory ->getCategoryUrl ($ category ),
166
- 'has_active ' => in_array ((string )$ categoryId , explode ('/ ' , (string )$ currentCategory ->getPath ()), true ),
167
- 'is_active ' => $ categoryId == $ currentCategory ->getId (),
168
138
'is_category ' => true ,
169
139
'is_parent_active ' => $ isParentActive
170
140
];
@@ -196,22 +166,4 @@ protected function getCategoryTree($storeId, $rootId)
196
166
197
167
return $ collection ;
198
168
}
199
-
200
- /**
201
- * Add active
202
- *
203
- * @param \Magento\Theme\Block\Html\Topmenu $subject
204
- * @param string[] $result
205
- * @return string[]
206
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
207
- */
208
- public function afterGetCacheKeyInfo (\Magento \Theme \Block \Html \Topmenu $ subject , array $ result )
209
- {
210
- $ activeCategory = $ this ->getCurrentCategory ();
211
- if ($ activeCategory ) {
212
- $ result [] = Category::CACHE_TAG . '_ ' . $ activeCategory ->getId ();
213
- }
214
-
215
- return $ result ;
216
- }
217
169
}
0 commit comments