3 namespace Drupal\Core\Menu;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\CacheCollector;
7 use Drupal\Core\Lock\LockBackendInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
11 * Provides the default implementation of the active menu trail service.
13 * It uses the current route name and route parameters to compare with the ones
16 class MenuActiveTrail extends CacheCollector implements MenuActiveTrailInterface {
19 * The menu link plugin manager.
21 * @var \Drupal\Core\Menu\MenuLinkManagerInterface
23 protected $menuLinkManager;
26 * The route match object for the current page.
28 * @var \Drupal\Core\Routing\RouteMatchInterface
30 protected $routeMatch;
33 * Constructs a \Drupal\Core\Menu\MenuActiveTrail object.
35 * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
36 * The menu link plugin manager.
37 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
38 * A route match object for finding the active link.
39 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
41 * @param \Drupal\Core\Lock\LockBackendInterface $lock
44 public function __construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock) {
45 parent::__construct(NULL, $cache, $lock);
46 $this->menuLinkManager = $menu_link_manager;
47 $this->routeMatch = $route_match;
53 * @see ::getActiveTrailIds()
55 protected function getCid() {
56 if (!isset($this->cid)) {
57 $route_parameters = $this->routeMatch->getRawParameters()->all();
58 ksort($route_parameters);
59 return 'active-trail:route:' . $this->routeMatch->getRouteName() . ':route_parameters:' . serialize($route_parameters);
68 * @see ::getActiveTrailIds()
70 protected function resolveCacheMiss($menu_name) {
71 $this->storage[$menu_name] = $this->doGetActiveTrailIds($menu_name);
72 $this->tags[] = 'config:system.menu.' . $menu_name;
73 $this->persist($menu_name);
75 return $this->storage[$menu_name];
81 * This implementation caches all active trail IDs per route match for *all*
82 * menus whose active trails are calculated on that page. This ensures 1 cache
83 * get for all active trails per page load, rather than N.
85 * It uses the cache collector pattern to do this.
88 * @see \Drupal\Core\Cache\CacheCollectorInterface
89 * @see \Drupal\Core\Cache\CacheCollector
91 public function getActiveTrailIds($menu_name) {
92 return $this->get($menu_name);
96 * Helper method for ::getActiveTrailIds().
98 protected function doGetActiveTrailIds($menu_name) {
99 // Parent ids; used both as key and value to ensure uniqueness.
100 // We always want all the top-level links with parent == ''.
101 $active_trail = ['' => ''];
103 // If a link in the given menu indeed matches the route, then use it to
104 // complete the active trail.
105 if ($active_link = $this->getActiveLink($menu_name)) {
106 if ($parents = $this->menuLinkManager->getParentIds($active_link->getPluginId())) {
107 $active_trail = $parents + $active_trail;
111 return $active_trail;
117 public function getActiveLink($menu_name = NULL) {
118 // Note: this is a very simple implementation. If you need more control
119 // over the return value, such as matching a prioritized list of menu names,
120 // you should substitute your own implementation for the 'menu.active_trail'
121 // service in the container.
122 // The menu links coming from the storage are already sorted by depth,
126 $route_name = $this->routeMatch->getRouteName();
127 // On a default (not custom) 403 page the route name is NULL. On a custom
128 // 403 page we will get the route name for that page, so we can consider
129 // it a feature that a relevant menu tree may be displayed.
131 $route_parameters = $this->routeMatch->getRawParameters()->all();
133 // Load links matching this route.
134 $links = $this->menuLinkManager->loadLinksByRoute($route_name, $route_parameters, $menu_name);
135 // Select the first matching link.
137 $found = reset($links);