function menu_tokens in Token 7
Implements hook_tokens() on behalf of menu.module.
File
- ./
token.tokens.inc, line 1086 - Token callbacks for the token module.
Code
function menu_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$url_options = array(
'absolute' => TRUE,
);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
// Node tokens.
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'menu-link':
if ($link = token_node_menu_link_load($node)) {
$replacements[$original] = $sanitize ? check_plain($link['title']) : $link['title'];
}
break;
}
// Chained token relationships.
if ($menu_tokens = token_find_with_prefix($tokens, 'menu-link')) {
if ($link = token_node_menu_link_load($node)) {
$replacements += token_generate('menu-link', $menu_tokens, array(
'menu-link' => $link,
), $options);
}
}
}
}
// Menu link tokens.
if ($type == 'menu-link' && !empty($data['menu-link'])) {
$link = (array) $data['menu-link'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'menu':
if ($menu = menu_load($link['menu_name'])) {
$replacements[$original] = $sanitize ? check_plain($menu['title']) : $menu['title'];
}
break;
case 'edit-url':
$replacements[$original] = url("admin/structure/menu/item/{$link['mlid']}/edit", $url_options);
break;
}
}
// Chained token relationships.
if (($menu_tokens = token_find_with_prefix($tokens, 'menu')) && ($menu = menu_load($link['menu_name']))) {
$replacements += token_generate('menu', $menu_tokens, array(
'menu' => $menu,
), $options);
}
}
// Menu tokens.
if ($type == 'menu' && !empty($data['menu'])) {
$menu = (array) $data['menu'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'name':
$replacements[$original] = $sanitize ? check_plain($menu['title']) : $menu['title'];
break;
case 'machine-name':
// This is a machine name so does not ever need to be sanitized.
$replacements[$original] = $menu['menu_name'];
break;
case 'description':
$replacements[$original] = $sanitize ? filter_xss($menu['description']) : $menu['description'];
break;
case 'menu-link-count':
$query = db_select('menu_links');
$query
->condition('menu_name', $menu['menu_name']);
$query
->addTag('menu_menu_link_count');
$count = $query
->countQuery()
->execute()
->fetchField();
$replacements[$original] = (int) $count;
break;
case 'edit-url':
$replacements[$original] = url("admin/structure/menu/manage/" . $menu['menu_name'], $url_options);
break;
}
}
}
return $replacements;
}