Implements hook_tokens().
function user_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array(
'absolute' => TRUE,
);
if (isset($options['langcode'])) {
$url_options['language'] = language_load($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'user' && !empty($data['user'])) {
$account = $data['user'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Basic user account information.
case 'uid':
// In the case of hook user_presave uid is not set yet.
$replacements[$original] = !empty($account->uid) ? $account->uid : t('not yet assigned');
break;
case 'name':
$name = user_format_name($account);
$replacements[$original] = $sanitize ? check_plain($name) : $name;
break;
case 'mail':
$replacements[$original] = $sanitize ? check_plain($account->mail) : $account->mail;
break;
case 'url':
$replacements[$original] = !empty($account->uid) ? url("user/{$account->uid}", $url_options) : t('not yet assigned');
break;
case 'edit-url':
$replacements[$original] = !empty($account->uid) ? url("user/{$account->uid}/edit", $url_options) : t('not yet assigned');
break;
// These tokens are default variations on the chained tokens handled below.
case 'last-login':
$replacements[$original] = !empty($account->login) ? format_date($account->login, 'medium', '', NULL, $langcode) : t('never');
break;
case 'created':
// In the case of user_presave the created date may not yet be set.
$replacements[$original] = !empty($account->created) ? format_date($account->created, 'medium', '', NULL, $langcode) : t('not yet created');
break;
}
}
if ($login_tokens = token_find_with_prefix($tokens, 'last-login')) {
$replacements += token_generate('date', $login_tokens, array(
'date' => $account->login,
), $options);
}
if ($registered_tokens = token_find_with_prefix($tokens, 'created')) {
$replacements += token_generate('date', $registered_tokens, array(
'date' => $account->created,
), $options);
}
}
if ($type == 'current-user') {
$account = user_load($GLOBALS['user']->uid);
$replacements += token_generate('user', $tokens, array(
'user' => $account,
), $options);
}
return $replacements;
}