You are here

function _token_menu_link_best_match in Token 8

Returns a best matched link for a given node.

If the url exists in multiple menus, default to the one set on the node itself.

Parameters

\Drupal\node\NodeInterface $node: The node to look up the default menu settings from.

array $links: An array of instances keyed by plugin ID.

Return value

\Drupal\Core\Menu\MenuLinkInterface A Link instance.

1 call to _token_menu_link_best_match()
menu_ui_tokens in ./token.tokens.inc
Implements hook_tokens() on behalf of menu_ui.module.

File

./token.tokens.inc, line 1477
Token callbacks for the token module.

Code

function _token_menu_link_best_match(NodeInterface $node, array $links) {

  // Get the menu ui defaults so we can determine what menu was
  // selected for this node. This ensures that if the node was added
  // to the menu via the node UI, we use that as a default. If it
  // was not added via the node UI then grab the first in the
  // retrieved array.
  $defaults = menu_ui_get_menu_link_defaults($node);
  if (isset($defaults['id']) && isset($links[$defaults['id']])) {
    $link = $links[$defaults['id']];
  }
  else {
    $link = reset($links);
  }
  return $link;
}