5
5
*/
6
6
namespace Magento \Catalog \Model ;
7
7
8
+ /**
9
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10
+ */
8
11
class Observer
9
12
{
10
13
/**
@@ -62,16 +65,23 @@ class Observer
62
65
protected $ _productResourceFactory ;
63
66
64
67
/**
65
- * @param \Magento\Catalog\Model\Resource\Category $categoryResource
66
- * @param \Magento\Catalog\Model\Resource\Product $catalogProduct
68
+ * @var \Magento\Framework\Registry
69
+ */
70
+ protected $ _registry ;
71
+
72
+ /**
73
+ * @param \Magento\Framework\Registry $registry
74
+ * @param Resource\Category $categoryResource
75
+ * @param Resource\Product $catalogProduct
67
76
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
68
- * @param \Magento\Catalog\Model\ Layer\Resolver $layerResolver
77
+ * @param Layer\Resolver $layerResolver
69
78
* @param \Magento\Catalog\Helper\Category $catalogCategory
70
79
* @param \Magento\Catalog\Helper\Data $catalogData
71
80
* @param Indexer\Category\Flat\State $categoryFlatState
72
- * @param \Magento\Catalog\Model\ Resource\ProductFactory $productResourceFactory
81
+ * @param Resource\ProductFactory $productResourceFactory
73
82
*/
74
83
public function __construct (
84
+ \Magento \Framework \Registry $ registry ,
75
85
\Magento \Catalog \Model \Resource \Category $ categoryResource ,
76
86
\Magento \Catalog \Model \Resource \Product $ catalogProduct ,
77
87
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
@@ -81,6 +91,7 @@ public function __construct(
81
91
\Magento \Catalog \Model \Indexer \Category \Flat \State $ categoryFlatState ,
82
92
\Magento \Catalog \Model \Resource \ProductFactory $ productResourceFactory
83
93
) {
94
+ $ this ->_registry = $ registry ;
84
95
$ this ->_categoryResource = $ categoryResource ;
85
96
$ this ->_catalogProduct = $ catalogProduct ;
86
97
$ this ->_storeManager = $ storeManager ;
@@ -137,11 +148,20 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
137
148
$ block ->addIdentity (\Magento \Catalog \Model \Category::CACHE_TAG . '_ ' . $ category ->getId ());
138
149
139
150
$ tree = $ parentCategoryNode ->getTree ();
151
+
152
+ $ isActiveCategory = false ;
153
+ /** @var \Magento\Catalog\Model\Category $currentCategory */
154
+ $ currentCategory = $ this ->_registry ->registry ('current_category ' );
155
+ if ($ currentCategory && $ currentCategory ->getId () == $ category ->getId ()) {
156
+ $ isActiveCategory = true ;
157
+ }
158
+
140
159
$ categoryData = [
141
160
'name ' => $ category ->getName (),
142
161
'id ' => $ nodeId ,
143
162
'url ' => $ this ->_catalogCategory ->getCategoryUrl ($ category ),
144
- 'is_active ' => $ this ->_isActiveMenuCategory ($ category ),
163
+ 'has_active ' => $ this ->hasActive ($ category ),
164
+ 'is_active ' => $ isActiveCategory
145
165
];
146
166
$ categoryNode = new \Magento \Framework \Data \Tree \Node ($ categoryData , 'id ' , $ tree , $ parentCategoryNode );
147
167
$ parentCategoryNode ->addChild ($ categoryNode );
@@ -162,7 +182,7 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
162
182
* @param \Magento\Framework\Data\Tree\Node $category
163
183
* @return bool
164
184
*/
165
- protected function _isActiveMenuCategory ($ category )
185
+ protected function hasActive ($ category )
166
186
{
167
187
if (!$ this ->_catalogLayer ) {
168
188
return false ;
0 commit comments