';
$output .= '
' . t('List of the currently available tokens on this site') . '';
$output .= '' . theme('token_tree', array('token_types' => 'all', 'click_insert' => FALSE, 'show_restricted' => TRUE)) . '';
$output .= '';
return $output;
}
}
/**
* Return an array of the core modules supported by token.module.
*/
function _token_core_supported_modules() {
return array('book', 'menu');
}
/**
* Implements hook_menu().
*/
function token_menu() {
/*$items['token/autocomplete/all/%menu_tail'] = array(
'page callback' => 'token_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'token.pages.inc',
);*/
$items['token/autocomplete/%token_type'] = array(
'page callback' => 'token_autocomplete_token',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'token.pages.inc',
);
/*$items['token/autocomplete/%token_type/%menu_tail'] = array(
'page callback' => 'token_autocomplete_token',
'page arguments' => array(2, 3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'token.pages.inc',
);*/
// Devel token pages.
if (module_exists('devel')) {
$items['node/%node/devel/token'] = array(
'title' => 'Tokens',
'page callback' => 'token_devel_token_object',
'page arguments' => array('node', 1),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'file' => 'token.pages.inc',
'weight' => 5,
);
$items['comment/%comment/devel/token'] = array(
'title' => 'Tokens',
'page callback' => 'token_devel_token_object',
'page arguments' => array('comment', 1),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'file' => 'token.pages.inc',
'weight' => 5,
);
$items['taxonomy/term/%taxonomy_term/devel/token'] = array(
'title' => 'Tokens',
'page callback' => 'token_devel_token_object',
'page arguments' => array('taxonomy_term', 2),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'file' => 'token.pages.inc',
'weight' => 5,
);
$items['user/%user/devel/token'] = array(
'title' => 'Tokens',
'page callback' => 'token_devel_token_object',
'page arguments' => array('user', 1),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'file' => 'token.pages.inc',
'weight' => 5,
);
}
return $items;
}
function token_type_load($token_type) {
$info = token_get_info();
return isset($info['types'][$token_type]) ? $info['types'][$token_type] : FALSE;
}
/**
* Implements hook_theme().
*/
function token_theme() {
return array(
'tree_table' => array(
'variables' => array('header' => array(), 'rows' => array(), 'attributes' => array(), 'empty' => '', 'caption' => ''),
'file' => 'token.pages.inc',
),
'token_tree' => array(
'variables' => array('token_types' => array(), 'global_types' => TRUE, 'click_insert' => TRUE, 'show_restricted' => FALSE, 'recursion_limit' => 4),
'file' => 'token.pages.inc',
),
);
}
/**
* Implements hook_library().
*/
function token_library() {
// jQuery treeTable plugin.
$libraries['treeTable'] = array(
'title' => 'jQuery treeTable',
'website' => 'http://plugins.jquery.com/project/treetable',
'version' => '2.3.0',
'js' => array(
drupal_get_path('module', 'token') . '/jquery.treeTable.js' => array(),
),
'css' => array(
drupal_get_path('module', 'token') . '/jquery.treeTable.css' => array(),
),
);
return $libraries;
}
/**
* Return an array of entity type to token type mappings.
*/
function token_get_entity_mapping($value_type = 'token', $value = NULL) {
$mapping = array(
'comment' => 'comment',
'node' => 'node',
'file' => 'file',
'taxonomy_term' => 'term',
'taxonomy_vocabulary' => 'vocabulary',
'user' => 'user',
);
if (!isset($value)) {
return $mapping;
}
elseif ($value_type == 'token') {
return array_search($value, $mapping);
}
elseif ($value_type == 'entity') {
return isset($mapping[$value]) ? $mapping[$value] : FALSE;
}
}
/**
* Implements hook_entity_info_alter().
*
* Since some token types to do not match their entity type names, we have to
* map them to the proper type. This is purely for other modules' benefit.
*/
function token_entity_info_alter(&$info) {
$entity_token_types = token_get_entity_mapping();
foreach ($entity_token_types as $entity_type => $token_type) {
if (isset($info[$entity_type])) {
$info[$entity_type]['token type'] = $token_type;
}
}
}
/**
* Implements hook_module_implements_alter().
*
* Adds missing token support for core modules.
*/
function token_module_implements_alter(&$implementations, $hook) {
if ($hook == 'tokens' || $hook == 'token_info') {
foreach (_token_core_supported_modules() as $module) {
if (module_exists($module)) {
$implementations[$module] = TRUE;
}
}
// Move token.module to get included first since it is responsible for
// other modules.
unset($implementations['token']);
$implementations = array_merge(array('token' => 'tokens'), $implementations);
}
}
/**
* Implements hook_flush_caches().
*/
function token_flush_caches() {
return array('cache_token');
}
/**
* Retrieve, sort, store token info from the cache.
*/
function token_get_info($token_type = NULL) {
global $language;
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['token_info'] = &drupal_static(__FUNCTION__);
}
$token_info = &$drupal_static_fast['token_info'];
if (empty($token_info)) {
$cid = "info:{$language->language}";
if ($cache = cache_get($cid, 'cache_token')) {
$token_info = $cache->data;
}
else {
$token_info = token_info();
// Pre-sort tokens.
uasort($token_info['types'], 'token_asort_tokens');
foreach (array_keys($token_info['tokens']) as $type) {
uasort($token_info['tokens'][$type], 'token_asort_tokens');
}
// Add a 'type' value to each token type so we can properly use
// token_type_load().
foreach (array_keys($token_info['types']) as $type_key) {
if (!isset($token_info['types'][$type_key]['type'])) {
$token_info['types'][$type_key]['type'] = $type_key;
}
}
// Store info in cache for future use.
cache_set($cid, $token_info, 'cache_token');
}
}
if (empty($token_type)) {
return $token_info;
}
elseif (isset($token_info[$token_type])) {
return $token_info[$token_type];
}
}
/**
* uasort() callback to sort tokens by the 'name' property.
*/
function token_asort_tokens($token_a, $token_b) {
return strnatcmp($token_a['name'], $token_b['name']);
}
/**
* Get a list of token types that can be used without any context (global).
*
* @return
* An array of global token types.
*/
function token_get_global_token_types() {
$global_types = &drupal_static(__FUNCTION__, array());
if (empty($global_types)) {
$token_info = token_get_info();
foreach ($token_info['types'] as $type => $type_info) {
// If the token types has not specified that 'needs-data' => TRUE, then
// it is a global token type that will always be replaced in any context.
if (empty($type_info['needs-data'])) {
$global_types[] = $type;
}
}
}
return $global_types;
}
/**
* Validate an tokens in raw text based on possible contexts.
*
* @param $text
* A string with the raw text containing the raw tokens.
* @param $tokens
* An array of token types that will be used when token replacement is
* performed.
* @return
* An array with the invalid tokens in their original raw forms.
*/
function token_get_invalid_tokens_by_context($text, $valid_types = array()) {
// Add the token types that are always valid in global context.
$valid_types = array_merge($valid_types, token_get_global_token_types());
$invalid_tokens = array();
foreach (token_scan($text) as $type => $tokens) {
if (!in_array($type, $valid_types)) {
// If the token type is not a valid context, its tokens are invalid.
$invalid_tokens = array_merge($invalid_tokens, array_values($tokens));
}
else {
// Check each individual token for validity.
$invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($type, $tokens));
}
}
array_unique($invalid_tokens);
return $invalid_tokens;
}
/**
* Validate an array of tokens based on their token type.
*
* @param $type
* The type of tokens to validate (e.g. 'node', etc.)
* @param $tokens
* A keyed array of tokens, and their original raw form in the source text.
* @return
* An array with the invalid tokens in their original raw forms.
*/
function token_get_invalid_tokens($type, $tokens) {
$token_info = token_get_info();
$invalid_tokens = array();
foreach ($tokens as $token => $full_token) {
// Split token up if it has chains.
$parts = explode(':', $token, 2);
if (!isset($token_info['tokens'][$type][$parts[0]])) {
// This is an invalid token (not defined).
$invalid_tokens[] = $full_token;
}
elseif (count($parts) == 2) {
$sub_token_info = $token_info['tokens'][$type][$parts[0]];
if (!empty($sub_token_info['dynamic'])) {
// If this token has been flagged as a dynamic token, skip it.
continue;
}
elseif (empty($sub_token_info['type'])) {
// If the token has chains, but does not support it, it is invalid.
$invalid_tokens[] = $full_token;
}
else {
// Resursively check the chained tokens.
$sub_tokens = token_find_with_prefix(array($token => $full_token), $parts[0]);
$invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($sub_token_info['type'], $sub_tokens));
}
}
}
return $invalid_tokens;
}
/**
* Validate a form element that should have tokens in it.
*
* Form elements that want to add this validation should have the #token_types
* parameter defined.
*
* For example:
* @code
* $form['my_node_text_element'] = array(
* '#type' => 'textfield',
* '#title' => t('Some text to token-ize that has a node context.'),
* '#default_value' => 'The title of this node is [node:title].',
* '#element_validate' => array('token_element_validate_token_context'),
* '#token_types' => array('node'),
* );
* @endcode
*/
function token_element_validate_token_context(&$element, &$form_state) {
$value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
$invalid_tokens = token_get_invalid_tokens_by_context($value, $element['#token_types']);
if ($invalid_tokens) {
form_error($element, t('The %element-title is using the following invalid tokens: @invalid-tokens.', array('%element-title' => $element['#title'], '@invalid-tokens' => implode(', ', $invalid_tokens))));
}
return $element;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters the user e-mail fields to add token context validation and
* adds the token tree for a better token UI and selection.
*/
function token_form_user_admin_settings_alter(&$form, &$form_state) {
$email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
foreach (element_children($form) as $key) {
$element = &$form[$key];
// Remove the crummy default token help text.
if (!empty($element['#description'])) {
$element['#description'] = trim(str_replace($email_token_help, '', $element['#description']));
}
switch ($key) {
case 'email_admin_created':
case 'email_pending_approval':
case 'email_no_approval_required':
case 'email_password_reset':
case 'email_cancel_confirm':
// Do nothing, but allow execution to continue.
break;
case 'email_activated':
case 'email_blocked':
case 'email_canceled':
// These fieldsets have their e-mail elements inside a 'settings'
// sub-element, so switch to that element instead.
$element = &$form[$key]['settings'];
break;
default:
continue 2;
}
foreach (element_children($element) as $sub_key) {
if (!isset($element[$sub_key]['#type'])) {
continue;
}
elseif ($element[$sub_key]['#type'] == 'textfield' && substr($sub_key, -8) === '_subject') {
// Add validation to subject textfields.
$element[$sub_key]['#element_validate'][] = 'token_element_validate_token_context';
$element[$sub_key] += array('#token_types' => array('user'));
}
elseif ($element[$sub_key]['#type'] == 'textarea' && substr($sub_key, -5) === '_body') {
// Add validation to body textareas.
$element[$sub_key]['#element_validate'][] = 'token_element_validate_token_context';
$element[$sub_key] += array('#token_types' => array('user'));
}
}
// Add the token tree UI.
$element['token_tree'] = array(
'#theme' => 'token_tree',
'#token_types' => array('user'),
'#show_restricted' => TRUE,
'#weight' => 100,
);
}
}
/**
* Build a tree array of tokens used for themeing or information.
*
* @param $token_type
* The token type.
* @param $flat_tree
* A boolean if TRUE will only make a flat array of tokens, otherwise
* child tokens will be inside the 'children' parameter of a token.
* @param $show_restricted
* A boolean if TRUE will show restricted tokens. Otherwise they will be
* hidden. Default is FALSE.
* @param $recursion_limit
* An integer with the maximum number of token levels to recurse.
* @param $parents
* An optional array with the current parents of the tokens.
*/
function token_build_tree($token_type, array $options = array()) {
global $language;
// Static cache of already built token trees.
$trees = &drupal_static(__FUNCTION__, array());
$options += array(
'restricted' => FALSE,
'depth' => 4,
'data' => array(),
'values' => FALSE,
'flat' => FALSE,
);
// Do not allow past the maximum token information depth.
$options['depth'] = min($options['depth'], TOKEN_MAX_DEPTH);
// If $token_type is an entity, make sure we are using the actual token type.
if ($entity_token_type = token_get_entity_mapping('entity', $token_type)) {
$token_type = $entity_token_type;
}
$tree_cid = "tree:{$token_type}:{$language->language}:{$options['depth']}";
// If we do not have this base tree in the static cache, check {cache_token}
// otherwise generate and store it in the cache.
if (!isset($trees[$tree_cid])) {
if ($cache = cache_get($tree_cid, 'cache_token')) {
$trees[$tree_cid] = $cache->data;
}
else {
$options['parents'] = array();
$trees[$tree_cid] = _token_build_tree($token_type, $options);
cache_set($tree_cid, $trees[$tree_cid], 'cache_token');
}
}
$tree = $trees[$tree_cid];
// If the user has requested a flat tree, convert it.
if (!empty($options['flat'])) {
$tree = token_flatten_tree($tree);
}
// Fill in token values.
if (!empty($options['values'])) {
$token_values = array();
foreach ($tree as $token => $token_info) {
if (!empty($token_info['dynamic']) || !empty($token_info['restricted'])) {
continue;
}
elseif (!isset($token_info['value'])) {
$token_values[$token_info['token']] = $token;
}
}
if (!empty($token_values)) {
$token_values = token_generate($token_type, $token_values, $options['data']);
foreach ($token_values as $token => $replacement) {
$tree[$token]['value'] = $replacement;
}
}
}
return $tree;
}
/**
* Flatten a token tree.
*/
function token_flatten_tree($tree) {
$result = array();
foreach ($tree as $token => $token_info) {
$result[$token] = $token_info;
if (isset($token_info['children']) && is_array($token_info['children'])) {
$result += token_flatten_tree($token_info['children']);
//unset($result[$token]['children']);
}
}
return $result;
}
/**
* Generate a token tree.
*/
function _token_build_tree($token_type, array $options) {
$options += array(
'parents' => array(),
);
$info = token_get_info();
if ($options['depth'] <= 0 || !isset($info['types'][$token_type])) {
return array();
}
$tree = array();
$root_token_type = $info['types'][$token_type]['type'];
foreach ($info['tokens'][$root_token_type] as $token => $token_info) {
// Build the raw token string.
$token_parents = $options['parents'];
if (empty($token_parents)) {
// If the parents array is currently empty, assume the token type is its
// parent.
$token_parents[] = $token_type;
}
$token_parents[] = $token;
if (!empty($token_info['dynamic'])) {
$token_parents[] = '?';
}
$raw_token = '[' . implode(':', $token_parents) . ']';
$tree[$raw_token] = $token_info;
$tree[$raw_token]['raw token'] = $raw_token;
// Add the token's real name (leave out the base token type).
$tree[$raw_token]['token'] = implode(':', array_slice($token_parents, 1));
// Add the token's parent as its raw token value.
if (!empty($options['parents'])) {
$tree[$raw_token]['parent'] = '[' . implode(':', $options['parents']) . ']';
}
// Fetch the child tokens.
if (!empty($token_info['type'])) {
$child_options = $options;
$child_options['depth']--;
$child_options['parents'] = $token_parents;
$tree[$raw_token]['children'] = _token_build_tree($token_info['type'], $child_options);
}
}
return $tree;
}
/**
* Find tokens that have been declared twice by different modules.
*/
function token_find_duplicate_tokens() {
$all_tokens = array();
foreach (module_implements('token_info') as $module) {
$module_token_info = module_invoke($module, 'token_info');
if (in_array($module, _token_core_supported_modules())) {
$module = 'token';
}
if (!empty($module_token_info['types'])) {
foreach (array_keys($module_token_info['types']) as $type) {
$all_tokens['type:' . $type][] = $module;
}
}
if (!empty($module_token_info['tokens'])) {
foreach ($module_token_info['tokens'] as $type => $tokens) {
foreach (array_keys($tokens) as $token) {
$all_tokens[$type . ':' . $token][] = $module;
}
}
}
}
foreach ($all_tokens as $token => $modules) {
if (count($modules) < 2) {
unset($all_tokens[$token]);
}
}
return $all_tokens;
}