Skip to content

Commit 092594b

Browse files
committed
MAGETWO-34180: Active item in Nivagation menu is not highlighted
1 parent f318191 commit 092594b

File tree

5 files changed

+187
-44
lines changed

5 files changed

+187
-44
lines changed

app/code/Magento/Catalog/Model/Observer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
142142
'id' => $nodeId,
143143
'url' => $this->_catalogCategory->getCategoryUrl($category),
144144
'is_active' => $this->_isActiveMenuCategory($category),
145+
'is_current_category' => $category->getIsCurrentCategory()
145146
];
146147
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
147148
$parentCategoryNode->addChild($categoryNode);

app/code/Magento/Catalog/Model/Resource/Category/Tree.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
*/
66
namespace Magento\Catalog\Model\Resource\Category;
77

8+
use Magento\Framework\Registry;
9+
use Magento\Catalog\Model\Resource\Category;
10+
use Magento\Framework\App\CacheInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Framework\App\Resource;
13+
use Magento\Framework\Event\ManagerInterface;
14+
use Magento\Catalog\Model\Attribute\Config;
15+
use Magento\Catalog\Model\Resource\Category\Collection\Factory;
16+
817
/**
918
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1019
*/
@@ -87,26 +96,35 @@ class Tree extends \Magento\Framework\Data\Tree\Dbp
8796
*/
8897
protected $_catalogCategory;
8998

99+
/**
100+
* @var Registry
101+
*/
102+
protected $registry;
103+
90104
/**
91105
* Construct
92106
*
93-
* @param \Magento\Catalog\Model\Resource\Category $catalogCategory
94-
* @param \Magento\Framework\App\CacheInterface $cache
95-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
96-
* @param \Magento\Framework\App\Resource $resource
97-
* @param \Magento\Framework\Event\ManagerInterface $eventManager
98-
* @param \Magento\Catalog\Model\Attribute\Config $attributeConfig
99-
* @param \Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
107+
* @param Registry $registry
108+
* @param Category $catalogCategory
109+
* @param CacheInterface $cache
110+
* @param StoreManagerInterface $storeManager
111+
* @param Resource $resource
112+
* @param ManagerInterface $eventManager
113+
* @param Config $attributeConfig
114+
* @param Factory $collectionFactory
115+
* @throws \Exception
100116
*/
101117
public function __construct(
102-
\Magento\Catalog\Model\Resource\Category $catalogCategory,
103-
\Magento\Framework\App\CacheInterface $cache,
104-
\Magento\Store\Model\StoreManagerInterface $storeManager,
105-
\Magento\Framework\App\Resource $resource,
106-
\Magento\Framework\Event\ManagerInterface $eventManager,
107-
\Magento\Catalog\Model\Attribute\Config $attributeConfig,
108-
\Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
118+
Registry $registry,
119+
Category $catalogCategory,
120+
CacheInterface $cache,
121+
StoreManagerInterface $storeManager,
122+
Resource $resource,
123+
ManagerInterface $eventManager,
124+
Config $attributeConfig,
125+
Factory $collectionFactory
109126
) {
127+
$this->registry = $registry;
110128
$this->_catalogCategory = $catalogCategory;
111129
$this->_cache = $cache;
112130
$this->_storeManager = $storeManager;
@@ -217,9 +235,22 @@ public function addCollectionData(
217235
}
218236
}
219237

238+
$this->markNodeAsCurrentCategory();
239+
220240
return $this;
221241
}
222242

243+
/**
244+
* @return void
245+
*/
246+
protected function markNodeAsCurrentCategory()
247+
{
248+
$category = $this->registry->registry('current_category');
249+
if ($category && $category->getId()) {
250+
$this->getNodeById($category->getId())->setIsCurrentCategory(true);
251+
}
252+
}
253+
223254
/**
224255
* Add inactive categories ids
225256
*

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

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
use Magento\Framework\View\Block\IdentityInterface;
99
use Magento\Framework\View\Element\Template;
10-
use Magento\Framework\Registry;
10+
use Magento\Framework\Data\TreeFactory;
11+
use Magento\Framework\Data\Tree\Node;
12+
use Magento\Framework\Data\Tree\NodeFactory;
1113

1214
/**
1315
* Html page top menu block
@@ -26,7 +28,7 @@ class Topmenu extends Template implements IdentityInterface
2628
*
2729
* @var \Magento\Framework\Data\Tree\Node
2830
*/
29-
protected $_menu;
31+
protected $menu;
3032

3133
/**
3234
* Core registry
@@ -35,23 +37,26 @@ class Topmenu extends Template implements IdentityInterface
3537
*/
3638
protected $registry;
3739

40+
/**
41+
* @param Template\Context $context
42+
* @param NodeFactory $nodeFactory
43+
* @param TreeFactory $treeFactory
44+
* @param array $data
45+
*/
3846
public function __construct(
39-
Registry $registry,
4047
Template\Context $context,
48+
NodeFactory $nodeFactory,
49+
TreeFactory $treeFactory,
4150
array $data = []
4251
) {
43-
$this->registry = $registry;
4452
parent::__construct($context, $data);
45-
}
46-
47-
/**
48-
* Init top menu tree structure
49-
*
50-
* @return void
51-
*/
52-
public function _construct()
53-
{
54-
$this->_menu = new \Magento\Framework\Data\Tree\Node([], 'root', new \Magento\Framework\Data\Tree());
53+
$this->menu = $nodeFactory->create(
54+
[
55+
'data' => [],
56+
'idField' => 'root',
57+
'tree' => $treeFactory->create()
58+
]
59+
);
5560
}
5661

5762
/**
@@ -66,18 +71,18 @@ public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit =
6671
{
6772
$this->_eventManager->dispatch(
6873
'page_block_html_topmenu_gethtml_before',
69-
['menu' => $this->_menu, 'block' => $this]
74+
['menu' => $this->menu, 'block' => $this]
7075
);
7176

72-
$this->_menu->setOutermostClass($outermostClass);
73-
$this->_menu->setChildrenWrapClass($childrenWrapClass);
77+
$this->menu->setOutermostClass($outermostClass);
78+
$this->menu->setChildrenWrapClass($childrenWrapClass);
7479

75-
$html = $this->_getHtml($this->_menu, $childrenWrapClass, $limit);
80+
$html = $this->_getHtml($this->menu, $childrenWrapClass, $limit);
7681

7782
$transportObject = new \Magento\Framework\Object(['html' => $html]);
7883
$this->_eventManager->dispatch(
7984
'page_block_html_topmenu_gethtml_after',
80-
['menu' => $this->_menu, 'transportObject' => $transportObject]
85+
['menu' => $this->menu, 'transportObject' => $transportObject]
8186
);
8287
$html = $transportObject->getHtml();
8388

@@ -224,13 +229,13 @@ protected function _getHtml(
224229

225230
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
226231
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml(
227-
$child->getName()
228-
) . '</span></a>' . $this->_addSubMenu(
229-
$child,
230-
$childLevel,
231-
$childrenWrapClass,
232-
$limit
233-
) . '</li>';
232+
$child->getName()
233+
) . '</span></a>' . $this->_addSubMenu(
234+
$child,
235+
$childLevel,
236+
$childrenWrapClass,
237+
$limit
238+
) . '</li>';
234239
$itemPosition++;
235240
$counter++;
236241
}
@@ -283,17 +288,15 @@ protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
283288
$classes[] = 'level' . $item->getLevel();
284289
$classes[] = $item->getPositionClass();
285290

286-
$currentCategoryName = $this->registry->registry('current_category')->getName();
287-
288291
if ($item->getIsFirst()) {
289292
$classes[] = 'first';
290293
}
291294

292-
if ($item->getIsActive() && $currentCategoryName != $item->getName()) {
295+
if ($item->getIsActive() && !$item->getIsCurrentCategory()) {
293296
$classes[] = 'has-active';
294297
}
295298

296-
if ($currentCategoryName == $item->getName()) {
299+
if ($item->getIsCurrentCategory()) {
297300
$classes[] = 'active';
298301
}
299302

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Data\Tree;
8+
9+
use Magento\Framework\ObjectManagerInterface;
10+
11+
/**
12+
* Factory class for @see \Magento\Framework\Data\Tree\Node
13+
*/
14+
class NodeFactory
15+
{
16+
/**
17+
* Object Manager instance
18+
*
19+
* @var ObjectManagerInterface
20+
*/
21+
protected $objectManager;
22+
23+
/**
24+
* Instance name to create
25+
*
26+
* @var string
27+
*/
28+
protected $instanceName;
29+
30+
/**
31+
* Factory constructor
32+
*
33+
* @param ObjectManagerInterface $objectManager
34+
* @param string $instanceName
35+
*/
36+
public function __construct(
37+
ObjectManagerInterface $objectManager,
38+
$instanceName = '\\Magento\\Framework\\Data\\Tree\\Node'
39+
) {
40+
$this->objectManager = $objectManager;
41+
$this->instanceName = $instanceName;
42+
}
43+
44+
/**
45+
* Create class instance with specified parameters
46+
*
47+
* @param array $data
48+
* @return \Magento\Framework\Data\Tree\Node
49+
*/
50+
public function create(array $data = [])
51+
{
52+
return $this->objectManager->create($this->instanceName, $data);
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Data;
8+
9+
use Magento\Framework\ObjectManagerInterface;
10+
11+
/**
12+
* Factory class for @see \Magento\Framework\Data\Tree
13+
*/
14+
class TreeFactory
15+
{
16+
/**
17+
* Object Manager instance
18+
*
19+
* @var ObjectManagerInterface
20+
*/
21+
protected $objectManager = null;
22+
23+
/**
24+
* Instance name to create
25+
*
26+
* @var string
27+
*/
28+
protected $instanceName = null;
29+
30+
/**
31+
* Factory constructor
32+
*
33+
* @param ObjectManagerInterface $objectManager
34+
* @param string $instanceName
35+
*/
36+
public function __construct(
37+
ObjectManagerInterface $objectManager,
38+
$instanceName = '\\Magento\\Framework\\Data\\Tree'
39+
) {
40+
$this->objectManager = $objectManager;
41+
$this->instanceName = $instanceName;
42+
}
43+
44+
/**
45+
* Create class instance with specified parameters
46+
*
47+
* @param array $data
48+
* @return \Magento\Framework\Data\Tree
49+
*/
50+
public function create(array $data = [])
51+
{
52+
return $this->objectManager->create($this->instanceName, $data);
53+
}
54+
}

0 commit comments

Comments
 (0)