Skip to content

Commit

Permalink
Merge pull request #2081 from usagov/USAGOV-2095-menu-block-cache
Browse files Browse the repository at this point in the history
USAGOV-2095-menu-block-cache
  • Loading branch information
akf authored Nov 21, 2024
2 parents 4234bb5 + b276c7c commit e06c930
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function build(): array {
$crumbs = $this->menuLinkManager->getParentIds($active->getPluginId());
$items = $this->getMenuTreeItems($menuID, $crumbs, $active, maxLevels: -1);
$twigVars = $this->prepareMenuItemsForTemplate($items, $active);
return $this->renderItems($items, $twigVars);
return $this->renderItems($items, $twigVars, $menuID);

}

Expand All @@ -71,13 +71,13 @@ public function build(): array {
'#siblings_of_active_item' => [],
'#submenu' => $items['#items'],
];
return $this->renderItems($items, $twigVars);
return $this->renderItems($items, $twigVars, $menuID);
}

/**
* Returns the render array to theme the navigation lists.
*/
private function renderItems(array $items, array $twigVars): array {
private function renderItems(array $items, array $twigVars, string $menuID): array {

$node = $this->routeMatch->getParameter('node');
return array_merge(
Expand All @@ -88,8 +88,10 @@ private function renderItems(array $items, array $twigVars): array {
'#translations' => $this->translations,

// Ensure drupal knows this block should be cached per path.
// and when the menu changes
'#cache' => [
'contexts' => ['url.path', 'url.query_args'],
'tags' => ['config:system.menu.' . $menuID],
]
],
$twigVars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ private function buildFromMenu(string $menuID): array {
if ($active = $this->trail->getActiveLink($menuID)) {
$crumbs = $this->menuLinkManager->getParentIds($active->getPluginId());
$items = $this->getMenuTreeItems($menuID, $crumbs, $active);
return $this->renderItems($items, $active);
return $this->renderItems($items, $menuID, $active);
}

// We're not in the menu.
// Display first level of this menu.
$items = $this->getMenuTreeItems($menuID);
return $this->renderItems($items);
return $this->renderItems($items, $menuID);
}

/**
Expand All @@ -98,14 +98,15 @@ private function buildFromParentNodeId(string $menuID, $parentNodeID, bool $clos
'url' => $this->request->getPathInfo(),
'title' => $this->routeMatch->getParameter('node')->getTitle(),
];
return $this->renderItems($items, $active, $leaf);
return $this->renderItems($items, $menuID, $active, $leaf);
}

/**
* Returns the render array to theme the navigation lists.
*/
private function renderItems(
array $items,
string $menuID,
?MenuLinkInterface $active = NULL,
array $leaf = [],
): array {
Expand All @@ -126,8 +127,10 @@ private function renderItems(
];

// Ensure drupal knows this block should be cached per path.
// and when the menu changes
$theme['#cache'] = [
'contexts' => ['url.path', 'url.query_args'],
'tags' => ['config:system.menu.' . $menuID],
];
return $theme;
}
Expand Down

0 comments on commit e06c930

Please sign in to comment.