You are here

public function MenuPositionActiveTrail::getActiveLink in Menu Position 8

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 MenuActiveTrail::getActiveLink

File

src/Menu/MenuPositionActiveTrail.php, line 64

Class

MenuPositionActiveTrail
Menu Position active trail.

Namespace

Drupal\menu_position\Menu

Code

public function getActiveLink($menu_name = NULL) {

  // Get all the rules.
  $query = $this->entityTypeManager
    ->getStorage('menu_position_rule')
    ->getQuery();

  // Filter on the menu name if there is one.
  if (isset($menu_name)) {
    $query
      ->condition('menu_name', $menu_name);
  }
  $results = $query
    ->sort('weight')
    ->execute();
  $rules = $this->entityTypeManager
    ->getStorage('menu_position_rule')
    ->loadMultiple($results);

  // Iterate over the rules.
  foreach ($rules as $rule) {

    // This rule is active.
    if ($rule
      ->isActive()) {
      $menu_link = $this->menuLinkManager
        ->createInstance($rule
        ->getMenuLink());
      $active_menu_link = NULL;
      switch ($this->settings
        ->get('link_display')) {
        case 'child':

          // Set this menu link to active.
          $active_menu_link = $menu_link;
          break;
        case 'parent':
          $active_menu_link = $this->menuLinkManager
            ->createInstance($menu_link
            ->getParent());
          break;
        case 'none':
          $active_menu_link = NULL;
          break;
      }
      return $active_menu_link;
    }
  }

  // Default implementation takes here.
  return parent::getActiveLink($menu_name);
}