Skip to content

Commit f6e8697

Browse files
committed
Remove active category in the cache key
- Rollback removal of is_parent_active option as it is needed for not included in the menu categories;
1 parent 3595928 commit f6e8697

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

app/code/Magento/Catalog/Plugin/Block/Topmenu.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ public function beforeGetHtml(
9595
$parentCategoryNode = $mapping[$categoryParentId];
9696

9797
$categoryNode = new Node(
98-
$this->getCategoryAsArray($category),
98+
$this->getCategoryAsArray(
99+
$category,
100+
$category->getParentId() == $categoryParentId
101+
),
99102
'id',
100103
$parentCategoryNode->getTree(),
101104
$parentCategoryNode
@@ -131,17 +134,19 @@ public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
131134
/**
132135
* Convert category to array
133136
*
134-
* @param \Magento\Catalog\Model\Category $category
137+
* @param Category $category
138+
* @param bool $isParentActive
135139
* @return array
136140
*/
137-
private function getCategoryAsArray($category)
141+
private function getCategoryAsArray($category, $isParentActive): array
138142
{
139143
$categoryId = $category->getId();
140144
return [
141145
'name' => $category->getName(),
142146
'id' => 'category-node-' . $categoryId,
143147
'url' => $this->catalogCategory->getCategoryUrl($category),
144-
'is_category' => true
148+
'is_category' => true,
149+
'is_parent_active' => $isParentActive
145150
];
146151
}
147152

app/code/Magento/Theme/Block/Html/Topmenu.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Backend\Model\Menu;
99
use Magento\Framework\Data\Tree\Node;
10+
use Magento\Framework\Data\Tree\Node\Collection;
1011
use Magento\Framework\Data\Tree\NodeFactory;
1112
use Magento\Framework\Data\TreeFactory;
1213
use Magento\Framework\DataObject;
@@ -217,6 +218,7 @@ protected function _getHtml(
217218

218219
$children = $menuTree->getChildren();
219220
$childLevel = $this->getChildLevel($menuTree->getLevel());
221+
$this->removeChildrenWithoutActiveParent($children, $childLevel);
220222

221223
$counter = 1;
222224
$childrenCount = $children->count();
@@ -371,6 +373,23 @@ public function getMenu()
371373
return $this->_menu;
372374
}
373375

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+
374393
/**
375394
* Retrieve child level based on parent level
376395
*

0 commit comments

Comments
 (0)