You are here

public function MenuActiveTrail::getActiveLink in Zircon Profile 8

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

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

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 122
Contains \Drupal\Core\Menu\MenuActiveTrail.

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;
}