function MenuActiveTrail::getActiveLink

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/ProxyClass/Menu/MenuActiveTrail.php \Drupal\Core\ProxyClass\Menu\MenuActiveTrail::getActiveLink()
  2. 11.x core/lib/Drupal/Core/Menu/MenuActiveTrail.php \Drupal\Core\Menu\MenuActiveTrail::getActiveLink()

File

core/lib/Drupal/Core/Menu/MenuActiveTrail.php, line 117

Class

MenuActiveTrail
Provides the default implementation of the active menu trail service.

Namespace

Drupal\Core\Menu

Code

public function getActiveLink($menu_name = NULL) {
  // Note: this is a very simple implementation. If you need more control
  // over the return value, such as matching a prioritized list of menu names,
  // you should substitute your own implementation for the 'menu.active_trail'
  // service in the container.
  // The menu links coming from the storage are already sorted by depth,
  // weight and ID.
  $found = NULL;
  $route_name = $this->routeMatch
    ->getRouteName();
  // On a default (not custom) 403 page the route name is NULL. On a custom
  // 403 page we will get the route name for that page, so we can consider
  // it a feature that a relevant menu tree may be displayed.
  if ($route_name) {
    $route_parameters = $this->routeMatch
      ->getRawParameters()
      ->all();
    // Load links matching this route.
    $links = $this->menuLinkManager
      ->loadLinksByRoute($route_name, $route_parameters, $menu_name);
    // Select the first matching link.
    if ($links) {
      $found = reset($links);
    }
  }
  return $found;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.