2424class Menu extends Template implements DataObject \IdentityInterface
2525{
2626 const XML_SNOWMENU_GENERAL_CUSTOMER_GROUPS = 'snowmenu/general/customer_groups ' ;
27+ const XML_SNOWMENU_GENERAL_CACHE_TAGS = 'snowmenu/general/cache_tags ' ;
2728
2829 /**
2930 * @var MenuRepositoryInterface
@@ -84,6 +85,11 @@ class Menu extends Template implements DataObject\IdentityInterface
8485 */
8586 private $ httpContext ;
8687
88+ /**
89+ * @var array
90+ */
91+ private $ nodeTypeCaches = [];
92+
8793 /**
8894 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
8995 */
@@ -97,6 +103,7 @@ public function __construct(
97103 ImageFile $ imageFile ,
98104 Escaper $ escaper ,
99105 Context $ httpContext ,
106+ array $ nodeTypeCaches = [],
100107 array $ data = []
101108 ) {
102109 parent ::__construct ($ context , $ data );
@@ -110,6 +117,7 @@ public function __construct(
110117 $ this ->setTemplate ($ this ->getMenuTemplate ($ this ->_template ));
111118 $ this ->submenuTemplate = $ this ->getSubmenuTemplate ();
112119 $ this ->httpContext = $ httpContext ;
120+ $ this ->nodeTypeCaches = $ nodeTypeCaches ;
113121 }
114122
115123 /**
@@ -119,11 +127,22 @@ public function __construct(
119127 */
120128 public function getIdentities ()
121129 {
122- return [
130+ $ tags = [
123131 \Snowdog \Menu \Model \Menu::CACHE_TAG . '_ ' . $ this ->loadMenu ()->getId (),
124132 Block::CACHE_TAG ,
125133 \Snowdog \Menu \Model \Menu::CACHE_TAG
126134 ];
135+ if (!$ this ->canGatherEntityCacheTags ()) {
136+ return $ tags ;
137+ }
138+ $ otherCacheTagsArrays = [];
139+ foreach ($ this ->nodeTypeCaches as $ provider ) {
140+ $ entityCacheTags = $ this ->nodeTypeProvider ->getProvider ($ provider )->getEntityCacheTags ();
141+ if (!empty ($ entityCacheTags )) {
142+ $ otherCacheTagsArrays [] = $ entityCacheTags ;
143+ }
144+ }
145+ return array_merge ($ tags , ...$ otherCacheTagsArrays );
127146 }
128147
129148 protected function getCacheLifetime ()
@@ -440,6 +459,9 @@ private function getSubmenuBlock($nodes, $parentNode, $level = 0)
440459 return $ block ;
441460 }
442461
462+ /**
463+ * @SuppressWarnings(PHPMD.NPathComplexity)
464+ */
443465 private function fetchData ()
444466 {
445467 $ nodes = $ this ->nodeRepository ->getByMenu ($ this ->loadMenu ()->getId ());
@@ -464,16 +486,29 @@ private function fetchData()
464486 $ result [$ level ][$ parent ] = [];
465487 }
466488 $ result [$ level ][$ parent ][] = $ node ;
489+ $ idx = array_key_last ($ result [$ level ][$ parent ]);
467490 $ type = $ node ->getType ();
468491 if (!isset ($ types [$ type ])) {
469492 $ types [$ type ] = [];
470493 }
471- $ types [$ type ][] = $ node ;
494+ $ types [$ type ][] = [
495+ 'node ' => $ node ,
496+ 'path ' => [$ level , $ parent , $ idx ]
497+ ];
472498 }
473499 $ this ->nodes = $ result ;
474500
475501 foreach ($ types as $ type => $ nodes ) {
476- $ this ->nodeTypeProvider ->prepareData ($ type , $ nodes );
502+ $ this ->nodeTypeProvider ->prepareData ($ type , array_column ($ nodes , 'node ' ));
503+ }
504+
505+ foreach ($ types ['category ' ] ?? [] as $ nodes ) {
506+ $ categoryProvider = $ this ->nodeTypeProvider ->getProvider ('category ' );
507+ $ productCount = $ categoryProvider ->getCategoryProductCount ($ nodes ['node ' ]->getNodeId ());
508+ if (empty ($ productCount ) && $ nodes ['node ' ]->getHideIfEmpty ()) {
509+ [$ level , $ parent , $ idx ] = $ nodes ['path ' ];
510+ unset($ this ->nodes [$ level ][$ parent ][$ idx ]);
511+ }
477512 }
478513 }
479514
@@ -509,6 +544,15 @@ private function getSubmenuTemplate()
509544 return $ this ->getMenuTemplate ($ baseSubmenuTemplate );
510545 }
511546
547+ private function canGatherEntityCacheTags ()
548+ {
549+ if (!$ this ->_scopeConfig ->isSetFlag (self ::XML_SNOWMENU_GENERAL_CACHE_TAGS )) {
550+ return false ;
551+ }
552+
553+ return !empty ($ this ->nodeTypeCaches );
554+ }
555+
512556 public function getCustomerGroupId ()
513557 {
514558 return $ this ->httpContext ->getValue (\Magento \Customer \Model \Context::CONTEXT_GROUP );
0 commit comments