From 87bfa945f767e2dfbfcf503cd0c55115746ab14f Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Thu, 14 Mar 2024 12:22:33 +0100 Subject: [PATCH] Add placeholder option to drupal_menu() --- src/TwigTweakExtension.php | 4 +-- src/View/MenuViewBuilder.php | 54 +++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index 36e3004..aebdd9b 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -209,8 +209,8 @@ class TwigTweakExtension extends AbstractExtension { /** * Returns the render array for Drupal menu. */ - public static function drupalMenu(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE): array { - return \Drupal::service('twig_tweak.menu_view_builder')->build($menu_name, $level, $depth, $expand); + public static function drupalMenu(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE, bool $placeholder = FALSE): array { + return \Drupal::service('twig_tweak.menu_view_builder')->build($menu_name, $level, $depth, $expand, $placeholder); } /** diff --git a/src/View/MenuViewBuilder.php b/src/View/MenuViewBuilder.php index 1f91ee2..74298e9 100644 --- a/src/View/MenuViewBuilder.php +++ b/src/View/MenuViewBuilder.php @@ -2,12 +2,14 @@ namespace Drupal\twig_tweak\View; +use Drupal\Core\Cache\Cache; use Drupal\Core\Menu\MenuLinkTreeInterface; +use Drupal\Core\Security\TrustedCallbackInterface; /** * Menu view builder. */ -class MenuViewBuilder { +class MenuViewBuilder implements TrustedCallbackInterface { /** * The menu link tree service. @@ -40,7 +42,35 @@ class MenuViewBuilder { * * @see \Drupal\system\Plugin\Block\SystemMenuBlock::build() */ - public function build(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE): array { + public function build(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE, bool $placeholder = FALSE): array { + $cache = [ + 'keys' => [ + 'twig_tweak_menu', + $menu_name, + '[level]=' . $level, + '[depth]=' . $depth, + '[expand]=' . (int) $expand, + ], + 'contexts' => [ + 'route.menu_active_trails:' . $menu_name, + ], + 'tags' => [ + 'config:system.menu.' . $menu_name, + ] + ]; + + if ($placeholder) { + return [ + 'menu' => [ + '#lazy_builder' => [ + 'twig_tweak.menu_view_builder:build', + [$menu_name, $level, $depth, $expand, FALSE], + ], + '#cache' => $cache, + ], + ]; + } + $parameters = $this->menuLinkTree->getCurrentRouteMenuTreeParameters($menu_name); // Adjust the menu tree parameters based on the block's configuration. @@ -66,19 +96,17 @@ class MenuViewBuilder { $tree = $this->menuLinkTree->transform($tree, $manipulators); $build = $this->menuLinkTree->build($tree); - if (!isset($build['#cache']['keys'])) { - $build['#cache']['keys'] = [ - 'twig_tweak_menu', - $menu_name, - '[level]=' . $level, - '[depth]=' . $depth, - '[expand]=' . (int) $expand, - ]; - } - - $build['#cache']['contexts'][] = 'route.menu_active_trails:' . $menu_name; + $build['#cache']['keys'] = array_unique(array_merge($cache['keys'], $build['#cache']['keys'] ?? [])); + $build['#cache']['contexts'] = Cache::mergeContexts($cache['contexts'], $build['#cache']['contexts'] ?? []); + $build['#cache']['tags'] = Cache::mergeTags($cache['tags'], $build['#cache']['tags'] ?? []); return $build; } + /** + * {@inheritdoc} + */ + public static function trustedCallbacks(): array { + return ['build']; + } }