27 private $collectionFactory;
32 private $storeManager;
37 private $layerResolver;
51 \
Magento\Catalog\Model\Layer\Resolver $layerResolver
54 $this->collectionFactory = $categoryCollectionFactory;
56 $this->layerResolver = $layerResolver;
69 public function beforeGetHtml(
72 $childrenWrapClass =
'',
75 $rootId = $this->storeManager->getStore()->getRootCategoryId();
76 $storeId = $this->storeManager->getStore()->getId();
79 $currentCategory = $this->getCurrentCategory();
80 $mapping = [$rootId => $subject->getMenu()];
82 $categoryParentId =
$category->getParentId();
83 if (!isset($mapping[$categoryParentId])) {
85 foreach ($parentIds as $parentId) {
86 if (isset($mapping[$parentId])) {
87 $categoryParentId = $parentId;
93 $parentCategoryNode = $mapping[$categoryParentId];
95 $categoryNode =
new Node(
96 $this->getCategoryAsArray(
99 $category->getParentId() == $categoryParentId
102 $parentCategoryNode->getTree(),
105 $parentCategoryNode->addChild($categoryNode);
107 $mapping[
$category->getId()] = $categoryNode;
117 public function beforeGetIdentities(\
Magento\Theme\Block\Html\Topmenu $subject)
120 $rootId = $this->storeManager->getStore()->getRootCategoryId();
121 $storeId = $this->storeManager->getStore()->getId();
124 $mapping = [$rootId => $subject->getMenu()];
126 if (!isset($mapping[
$category->getParentId()])) {
138 private function getCurrentCategory()
140 $catalogLayer = $this->layerResolver->get();
142 if (!$catalogLayer) {
146 return $catalogLayer->getCurrentCategory();
157 private function getCategoryAsArray(
$category, $currentCategory, $isParentActive)
161 'id' =>
'category-node-' .
$category->getId(),
162 'url' => $this->catalogCategory->getCategoryUrl(
$category),
163 'has_active' => in_array((
string)
$category->getId(), explode(
'/', $currentCategory->getPath()),
true),
164 'is_active' =>
$category->getId() == $currentCategory->getId(),
165 'is_category' =>
true,
166 'is_parent_active' => $isParentActive
178 protected function getCategoryTree(
$storeId, $rootId)
184 $collection->addFieldToFilter(
'path', [
'like' =>
'1/' . $rootId .
'/%']);
185 $collection->addAttributeToFilter(
'include_in_menu', 1);