diff --git a/token.tokens.inc b/token.tokens.inc index fff19e8..12479df 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -1117,18 +1117,7 @@ function menu_ui_tokens($type, $tokens, array $data = array(), array $options = else { $url = $node->toUrl(); if ($links = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters())) { - // 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); - } + $link = _token_get_default_menu_link($node, $links); $replacements[$original] = token_menu_link_translated_title($link, $langcode); } } @@ -1144,18 +1133,7 @@ function menu_ui_tokens($type, $tokens, array $data = array(), array $options = else { $url = $node->urlInfo(); if ($links = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters())) { - // 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); - } + $link = _token_get_default_menu_link($node, $links); $replacements += \Drupal::token()->generate('menu-link', $menu_tokens, array('menu-link' => $link), $options, $bubbleable_metadata); } } @@ -1226,6 +1204,28 @@ function menu_ui_tokens($type, $tokens, array $data = array(), array $options = } /** + * Returns a default link for a given node. + * + * If the url exists in multiple menus, default to the one + * set on the node itself. + */ +function _token_get_default_menu_link($node, $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; +} + +/** * Implements hook_token_info_alter() on behalf of field.module. * * We use hook_token_info_alter() rather than hook_token_info() as other