function MenuActiveTrail::getActiveLink

Same name in this branch
  1. 11.x core/lib/Drupal/Core/ProxyClass/Menu/MenuActiveTrail.php \Drupal\Core\ProxyClass\Menu\MenuActiveTrail::getActiveLink()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Menu/MenuActiveTrail.php \Drupal\Core\Menu\MenuActiveTrail::getActiveLink()
  2. 8.9.x core/lib/Drupal/Core/Menu/MenuActiveTrail.php \Drupal\Core\Menu\MenuActiveTrail::getActiveLink()
  3. 10 core/lib/Drupal/Core/ProxyClass/Menu/MenuActiveTrail.php \Drupal\Core\ProxyClass\Menu\MenuActiveTrail::getActiveLink()
  4. 10 core/lib/Drupal/Core/Menu/MenuActiveTrail.php \Drupal\Core\Menu\MenuActiveTrail::getActiveLink()

Fetches a menu link that matches the currently active route.

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 currently active route, 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

1 call to MenuActiveTrail::getActiveLink()
MenuActiveTrail::doGetActiveTrailIds in core/lib/Drupal/Core/Menu/MenuActiveTrail.php
Helper method for ::getActiveTrailIds().

File

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

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;
  $links = [];
  $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);
  }
  // If the request is for the site's front page, then menu links containing
  // <front> must also be loaded since it's a special route that's an alias
  // for the page.
  // @todo Combine the two calls to loadLinksByRoute() in
  // https://www.drupal.org/project/drupal/issues/3523497
  if ($this->pathMatcher
    ->isFrontPage()) {
    $links = array_merge($links, $this->menuLinkManager
      ->loadLinksByRoute('<front>', [], $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.