File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -95,7 +95,10 @@ public function beforeGetHtml(
95
95
$ parentCategoryNode = $ mapping [$ categoryParentId ];
96
96
97
97
$ categoryNode = new Node (
98
- $ this ->getCategoryAsArray ($ category ),
98
+ $ this ->getCategoryAsArray (
99
+ $ category ,
100
+ $ category ->getParentId () == $ categoryParentId
101
+ ),
99
102
'id ' ,
100
103
$ parentCategoryNode ->getTree (),
101
104
$ parentCategoryNode
@@ -131,17 +134,19 @@ public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
131
134
/**
132
135
* Convert category to array
133
136
*
134
- * @param \Magento\Catalog\Model\Category $category
137
+ * @param Category $category
138
+ * @param bool $isParentActive
135
139
* @return array
136
140
*/
137
- private function getCategoryAsArray ($ category)
141
+ private function getCategoryAsArray ($ category, $ isParentActive ): array
138
142
{
139
143
$ categoryId = $ category ->getId ();
140
144
return [
141
145
'name ' => $ category ->getName (),
142
146
'id ' => 'category-node- ' . $ categoryId ,
143
147
'url ' => $ this ->catalogCategory ->getCategoryUrl ($ category ),
144
- 'is_category ' => true
148
+ 'is_category ' => true ,
149
+ 'is_parent_active ' => $ isParentActive
145
150
];
146
151
}
147
152
Original file line number Diff line number Diff line change 7
7
8
8
use Magento \Backend \Model \Menu ;
9
9
use Magento \Framework \Data \Tree \Node ;
10
+ use Magento \Framework \Data \Tree \Node \Collection ;
10
11
use Magento \Framework \Data \Tree \NodeFactory ;
11
12
use Magento \Framework \Data \TreeFactory ;
12
13
use Magento \Framework \DataObject ;
@@ -217,6 +218,7 @@ protected function _getHtml(
217
218
218
219
$ children = $ menuTree ->getChildren ();
219
220
$ childLevel = $ this ->getChildLevel ($ menuTree ->getLevel ());
221
+ $ this ->removeChildrenWithoutActiveParent ($ children , $ childLevel );
220
222
221
223
$ counter = 1 ;
222
224
$ childrenCount = $ children ->count ();
@@ -371,6 +373,23 @@ public function getMenu()
371
373
return $ this ->_menu ;
372
374
}
373
375
376
+ /**
377
+ * Remove children from collection when the parent is not active
378
+ *
379
+ * @param Collection $children
380
+ * @param int $childLevel
381
+ * @return void
382
+ */
383
+ private function removeChildrenWithoutActiveParent (Collection $ children , int $ childLevel ): void
384
+ {
385
+ /** @var Node $child */
386
+ foreach ($ children as $ child ) {
387
+ if ($ childLevel === 0 && $ child ->getData ('is_parent_active ' ) === false ) {
388
+ $ children ->delete ($ child );
389
+ }
390
+ }
391
+ }
392
+
374
393
/**
375
394
* Retrieve child level based on parent level
376
395
*
You can’t perform that action at this time.
0 commit comments