MenuActiveTrail::getActiveLink

public MenuActiveTrail::getActiveLink($menu_name = NULL)

Fetches a menu link which matches the route name, parameters and menu name.

Parameters

string|null $menu_name: (optional) The menu within which to find the active link. If omitted, all menus will be searched.

Return value

\Drupal\Core\Menu\MenuLinkInterface|null The menu link for the given route name, parameters and menu, or NULL if there is no matching menu link or the current user cannot access the current page (i.e. we have a 403 response).

Overrides MenuActiveTrailInterface::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;
}
doc_Drupal
2016-10-29 09:26:03
Comments
Leave a Comment

Please login to continue.