1212use Magento \Framework \Data \Tree \Node ;
1313
1414/**
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.
1617 */
1718class Topmenu
1819{
1920 /**
20- * Catalog category
21- *
2221 * @var \Magento\Catalog\Helper\Category
2322 */
2423 protected $ catalogCategory ;
@@ -33,29 +32,21 @@ class Topmenu
3332 */
3433 private $ storeManager ;
3534
36- /**
37- * @var \Magento\Catalog\Model\Layer\Resolver
38- */
39- private $ layerResolver ;
40-
4135 /**
4236 * Initialize dependencies.
4337 *
4438 * @param \Magento\Catalog\Helper\Category $catalogCategory
4539 * @param \Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory $categoryCollectionFactory
4640 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
47- * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
4841 */
4942 public function __construct (
5043 \Magento \Catalog \Helper \Category $ catalogCategory ,
5144 \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
5446 ) {
5547 $ this ->catalogCategory = $ catalogCategory ;
5648 $ this ->collectionFactory = $ categoryCollectionFactory ;
5749 $ this ->storeManager = $ storeManager ;
58- $ this ->layerResolver = $ layerResolver ;
5950 }
6051
6152 /**
@@ -78,7 +69,6 @@ public function beforeGetHtml(
7869 $ storeId = $ this ->storeManager ->getStore ()->getId ();
7970 /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
8071 $ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
81- $ currentCategory = $ this ->getCurrentCategory ();
8272 $ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
8373 foreach ($ collection as $ category ) {
8474 $ categoryParentId = $ category ->getParentId ();
@@ -97,7 +87,6 @@ public function beforeGetHtml(
9787 $ categoryNode = new Node (
9888 $ this ->getCategoryAsArray (
9989 $ category ,
100- $ currentCategory ,
10190 $ category ->getParentId () == $ categoryParentId
10291 ),
10392 'id ' ,
@@ -132,39 +121,20 @@ public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
132121 }
133122 }
134123
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-
151124 /**
152125 * Convert category to array
153126 *
154- * @param \Magento\Catalog\Model\Category $category
155- * @param \Magento\Catalog\Model\Category $currentCategory
127+ * @param Category $category
156128 * @param bool $isParentActive
157129 * @return array
158130 */
159- private function getCategoryAsArray ($ category , $ currentCategory , $ isParentActive )
131+ private function getCategoryAsArray ($ category , $ isParentActive ): array
160132 {
161133 $ categoryId = $ category ->getId ();
162134 return [
163135 'name ' => $ category ->getName (),
164136 'id ' => 'category-node- ' . $ categoryId ,
165137 'url ' => $ this ->catalogCategory ->getCategoryUrl ($ category ),
166- 'has_active ' => in_array ((string )$ categoryId , explode ('/ ' , (string )$ currentCategory ->getPath ()), true ),
167- 'is_active ' => $ categoryId == $ currentCategory ->getId (),
168138 'is_category ' => true ,
169139 'is_parent_active ' => $ isParentActive
170140 ];
@@ -196,22 +166,4 @@ protected function getCategoryTree($storeId, $rootId)
196166
197167 return $ collection ;
198168 }
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- }
217169}
0 commit comments