View source
<?php
function token_filter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Token filter'),
);
case 'description':
return t('Allows you to use token values in a filter enclosed. Eg. [token global site-name] or [token user user-name]');
case 'settings':
break;
case 'no cache':
return TRUE;
case 'prepare':
return $text;
case 'process':
if (strpos($text, "[token") === false) {
return $text;
}
else {
$output .= preg_replace_callback("|\\[token ([^ ]*)([^]]*)\\]|i", 'token_filter_replacetoken', $text);
return $output;
}
break;
default:
return $text;
}
}
function token_filter_replacetoken($matches) {
global $user;
$type = $matches[1];
$token = $matches[2];
switch ($type) {
case 'global':
$object = NULL;
break;
case 'user':
$object = $user;
break;
default:
drupal_set_message("Filter Error: token_filter doesn't yet handle objects of type {$type}", 'error');
return "%{$token}";
}
$output = token_replace($token, $type, $object = NULL, $leading = '', $trailing = '');
return $output;
}