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,26 @@ public function __construct(
58
57
}
59
58
60
59
/**
61
- * Checking whether the using static urls in WYSIWYG allowed event
60
+ * Built category tree for menu block.
62
61
*
63
- * @param \Magento\Framework\Event\Observer $observer
64
- * @return void
62
+ * @param \Magento\Theme\Block\Html\Topmenu $subject
63
+ * @param string $outermostClass
64
+ * @param string $childrenWrapClass
65
+ * @param int $limit
66
+ * @SuppressWarnings("PMD.UnusedFormalParameter")
65
67
*/
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
-
68
+ public function beforeGetHtml (
69
+ \ Magento \ Theme \ Block \ Html \ Topmenu $ subject ,
70
+ $ outermostClass = '' ,
71
+ $ childrenWrapClass = '' ,
72
+ $ limit = 0
73
+ ) {
72
74
$ rootId = $ this ->storeManager ->getStore ()->getRootCategoryId ();
73
75
$ storeId = $ this ->storeManager ->getStore ()->getId ();
74
-
75
76
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
76
77
$ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
77
-
78
78
$ currentCategory = $ this ->getCurrentCategory ();
79
-
80
- $ mapping = [$ rootId => $ menuRootNode ]; // use nodes stack to avoid recursion
79
+ $ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
81
80
foreach ($ collection as $ category ) {
82
81
if (!isset ($ mapping [$ category ->getParentId ()])) {
83
82
continue ;
@@ -94,8 +93,27 @@ public function execute(\Magento\Framework\Event\Observer $observer)
94
93
$ parentCategoryNode ->addChild ($ categoryNode );
95
94
96
95
$ mapping [$ category ->getId ()] = $ categoryNode ; //add node in stack
96
+ }
97
+ }
97
98
98
- $ block ->addIdentity (Category::CACHE_TAG . '_ ' . $ category ->getId ());
99
+ /**
100
+ * Add list of associated identities to the top menu block for caching purposes.
101
+ *
102
+ * @param \Magento\Theme\Block\Html\Topmenu $subject
103
+ */
104
+ public function beforeGetIdentities (\Magento \Theme \Block \Html \Topmenu $ subject )
105
+ {
106
+ $ subject ->addIdentity (Category::CACHE_TAG );
107
+ $ rootId = $ this ->storeManager ->getStore ()->getRootCategoryId ();
108
+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
109
+ /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
110
+ $ collection = $ this ->getCategoryTree ($ storeId , $ rootId );
111
+ $ mapping = [$ rootId => $ subject ->getMenu ()]; // use nodes stack to avoid recursion
112
+ foreach ($ collection as $ category ) {
113
+ if (!isset ($ mapping [$ category ->getParentId ()])) {
114
+ continue ;
115
+ }
116
+ $ subject ->addIdentity (Category::CACHE_TAG . '_ ' . $ category ->getId ());
99
117
}
100
118
}
101
119
0 commit comments