You are here

public function ContextActiveTrail::getActiveLink in Context Active Trail 8

Same name and namespace in other branches
  1. 8.2 src/ContextActiveTrail.php \Drupal\context_active_trail\ContextActiveTrail::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 MenuActiveTrail::getActiveLink

File

src/ContextActiveTrail.php, line 47

Class

ContextActiveTrail
Allow the active trail to be set manually.

Namespace

Drupal\context_active_trail

Code

public function getActiveLink($menu_name = NULL) {

  // Try to get the value from context.
  foreach ($this->contextManager
    ->getActiveReactions('active_trail') as $reaction) {
    if ($link_id = $reaction
      ->getLinkId()) {
      try {
        $instance = $this->menuLinkManager
          ->getInstance([
          'id' => $link_id,
        ]);
      } catch (PluginNotFoundException $e) {
        \Drupal::logger('context_active_trail')
          ->error('Could not find the configured menu link to set active: @error', [
          '@error' => $e
            ->getMessage(),
        ]);
        return FALSE;
      }

      // Check that the instance is in the menu.
      if (!empty($menu_name) && $instance
        ->getMenuName() != $menu_name) {
        break;
      }
      return $instance;
    }
  }

  // Fall back to the default.
  return parent::getActiveLink($menu_name);
}