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
{
@@ -78,7 +79,6 @@ public function beforeGetHtml(
78
79
$ storeId = $ this ->storeManager ->getStore ()->getId ();
79
80
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
80
81
$ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
81
- $ currentCategory = $ this ->getCurrentCategory ();
82
82
$ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
83
83
foreach ($ collection as $ category ) {
84
84
$ categoryParentId = $ category ->getParentId ();
@@ -95,11 +95,7 @@ public function beforeGetHtml(
95
95
$ parentCategoryNode = $ mapping [$ categoryParentId ];
96
96
97
97
$ categoryNode = new Node (
98
- $ this ->getCategoryAsArray (
99
- $ category ,
100
- $ currentCategory ,
101
- $ category ->getParentId () == $ categoryParentId
102
- ),
98
+ $ this ->getCategoryAsArray ($ category ),
103
99
'id ' ,
104
100
$ parentCategoryNode ->getTree (),
105
101
$ parentCategoryNode
@@ -132,41 +128,20 @@ public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
132
128
}
133
129
}
134
130
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
131
/**
152
132
* Convert category to array
153
133
*
154
134
* @param \Magento\Catalog\Model\Category $category
155
- * @param \Magento\Catalog\Model\Category $currentCategory
156
- * @param bool $isParentActive
157
135
* @return array
158
136
*/
159
- private function getCategoryAsArray ($ category, $ currentCategory , $ isParentActive )
137
+ private function getCategoryAsArray ($ category )
160
138
{
161
139
$ categoryId = $ category ->getId ();
162
140
return [
163
141
'name ' => $ category ->getName (),
164
142
'id ' => 'category-node- ' . $ categoryId ,
165
143
'url ' => $ this ->catalogCategory ->getCategoryUrl ($ category ),
166
- 'has_active ' => in_array ((string )$ categoryId , explode ('/ ' , (string )$ currentCategory ->getPath ()), true ),
167
- 'is_active ' => $ categoryId == $ currentCategory ->getId (),
168
- 'is_category ' => true ,
169
- 'is_parent_active ' => $ isParentActive
144
+ 'is_category ' => true
170
145
];
171
146
}
172
147
0 commit comments