3
3
* Copyright © 2016 Magento. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
- namespace Magento \Catalog \Observer ;
6
+ namespace Magento \Catalog \Plugin \ Block ;
7
7
8
8
use Magento \Catalog \Model \Category ;
9
9
use Magento \Framework \Data \Collection ;
10
10
use Magento \Framework \Data \Tree \Node ;
11
- use Magento \Framework \Event \ObserverInterface ;
12
11
13
12
/**
14
- * Observer that add Categories Tree to Topmenu
13
+ * Plugin for top menu block
15
14
*/
16
- class AddCatalogToTopmenuItemsObserver implements ObserverInterface
15
+ class Topmenu
17
16
{
18
17
/**
19
18
* Catalog category
@@ -38,11 +37,11 @@ class AddCatalogToTopmenuItemsObserver implements ObserverInterface
38
37
private $ layerResolver ;
39
38
40
39
/**
40
+ * Initialize dependencies.
41
+ *
41
42
* @param \Magento\Catalog\Helper\Category $catalogCategory
42
- * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState
43
43
* @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
44
44
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
45
- * @param \Magento\Catalog\Helper\Category $catalogCategory
46
45
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
47
46
*/
48
47
public function __construct (
@@ -58,26 +57,27 @@ public function __construct(
58
57
}
59
58
60
59
/**
61
- * Checking whether the using static urls in WYSIWYG allowed event
60
+ * Build category tree for menu block.
62
61
*
63
- * @param \Magento\Framework\Event\Observer $observer
62
+ * @param \Magento\Theme\Block\Html\Topmenu $subject
63
+ * @param string $outermostClass
64
+ * @param string $childrenWrapClass
65
+ * @param int $limit
64
66
* @return void
67
+ * @SuppressWarnings("PMD.UnusedFormalParameter")
65
68
*/
66
- public function execute ( \ Magento \ Framework \ Event \ Observer $ observer )
67
- {
68
- $ block = $ observer -> getEvent ()-> getBlock ();
69
- $ menuRootNode = $ observer -> getEvent ()-> getMenu ();
70
- $ block -> addIdentity (Category:: CACHE_TAG );
71
-
69
+ public function beforeGetHtml (
70
+ \ Magento \ Theme \ Block \ Html \ Topmenu $ subject ,
71
+ $ outermostClass = '' ,
72
+ $ childrenWrapClass = '' ,
73
+ $ limit = 0
74
+ ) {
72
75
$ rootId = $ this ->storeManager ->getStore ()->getRootCategoryId ();
73
76
$ storeId = $ this ->storeManager ->getStore ()->getId ();
74
-
75
77
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
76
78
$ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
77
-
78
79
$ currentCategory = $ this ->getCurrentCategory ();
79
-
80
- $ mapping = [$ rootId => $ menuRootNode ]; // use nodes stack to avoid recursion
80
+ $ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
81
81
foreach ($ collection as $ category ) {
82
82
if (!isset ($ mapping [$ category ->getParentId ()])) {
83
83
continue ;
@@ -94,8 +94,28 @@ public function execute(\Magento\Framework\Event\Observer $observer)
94
94
$ parentCategoryNode ->addChild ($ categoryNode );
95
95
96
96
$ mapping [$ category ->getId ()] = $ categoryNode ; //add node in stack
97
+ }
98
+ }
97
99
98
- $ block ->addIdentity (Category::CACHE_TAG . '_ ' . $ category ->getId ());
100
+ /**
101
+ * Add list of associated identities to the top menu block for caching purposes.
102
+ *
103
+ * @param \Magento\Theme\Block\Html\Topmenu $subject
104
+ * @return void
105
+ */
106
+ public function beforeGetIdentities (\Magento \Theme \Block \Html \Topmenu $ subject )
107
+ {
108
+ $ subject ->addIdentity (Category::CACHE_TAG );
109
+ $ rootId = $ this ->storeManager ->getStore ()->getRootCategoryId ();
110
+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
111
+ /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
112
+ $ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
113
+ $ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
114
+ foreach ($ collection as $ category ) {
115
+ if (!isset ($ mapping [$ category ->getParentId ()])) {
116
+ continue ;
117
+ }
118
+ $ subject ->addIdentity (Category::CACHE_TAG . '_ ' . $ category ->getId ());
99
119
}
100
120
}
101
121
0 commit comments