diff --git a/token.tokens.inc b/token.tokens.inc index fb3bee8..b901c72 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -1071,28 +1071,15 @@ function menu_tokens($type, $tokens, array $data = array(), array $options = arr // Node tokens. if ($type == 'node' && !empty($data['node'])) { - // We need to clone the node as menu_node_prepare() may cause data loss. - // @see http://drupal.org/node/1317926 - $node = clone $data['node']; - - if (!isset($node->menu)) { - // Nodes do not have their menu links loaded via menu_node_load(). - menu_node_prepare($node); - } - - if (!empty($node->menu['mlid'])) { - $link = token_menu_link_load($node->menu['mlid']); - - foreach ($tokens as $name => $original) { - switch ($name) { - case 'menu-link': - $replacements[$original] = $sanitize ? check_plain($link['title']) : $link['title']; - break; - } + $link = NULL; + foreach ($tokens as $name => $original) { + if ($name == 'menu-link' && $link = _token_node_load_menu($data['node'])) { + $replacements[$original] = $sanitize ? check_plain($link['title']) : $link['title']; } - - // Chained token relationships. - if ($menu_tokens = token_find_with_prefix($tokens, 'menu-link')) { + } + // Chained token relationships. + if ($menu_tokens = token_find_with_prefix($tokens, 'menu-link')) { + if ($link || ($link = _token_node_load_menu($data['node']))) { $replacements += token_generate('menu-link', $menu_tokens, array('menu-link' => $link), $options); } } @@ -1155,6 +1142,22 @@ function menu_tokens($type, $tokens, array $data = array(), array $options = arr } /** + * Cares about loading menu related data for a node. + */ +function _token_node_load_menu($node) { + if (!isset($node->menu)) { + // We need to clone the node as menu_node_prepare() may cause data loss. + // @see http://drupal.org/node/1317926 + $node = clone $node; + + // Nodes do not have their menu links loaded via menu_node_load(). + menu_node_prepare($node); + } + + return !empty($node->menu['mlid']) ? token_menu_link_load($node->menu['mlid']) : FALSE; +} + +/** * Implements hook_token_info() on behalf of profile.module. */ function profile_token_info() {