1 token_example.tokens.inc | token_example_tokens($type, $tokens, array $data = array(), array $options = array()) |
Implements hook_tokens().
Related topics
File
- modules/
examples/ token_example/ token_example.tokens.inc, line 61 - Token callbacks for the token_example module.
Code
function token_example_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
// Text format tokens.
if ($type == 'format' && !empty($data['format'])) {
$format = $data['format'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
// Since {filter_format}.format is an integer and not user-entered
// text, it does not need to ever be sanitized.
$replacements[$original] = $format->format;
break;
case 'name':
// Since the format name is user-entered text, sanitize when requested.
$replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
break;
}
}
}
// Default format tokens.
if ($type == 'default-format') {
$default_id = filter_default_format();
$default_format = filter_format_load($default_id);
$replacements += token_generate('format', $tokens, array('format' => $default_format), $options);
}
// Node tokens.
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'body-format':
if ($items = field_get_items('node', $node, 'body')) {
$format = filter_format_load($items[0]['format']);
$replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
}
break;
}
}
// Chained token relationships.
if ($format_tokens = token_find_with_prefix($tokens, 'body-format')) {
if ($items = field_get_items('node', $node, 'body')) {
$body_format = filter_format_load($items[0]['format']);
$replacements += token_generate('format', $format_tokens, array('format' => $body_format), $options);
}
}
}
// Comment tokens.
if ($type == 'comment' && !empty($data['comment'])) {
$comment = $data['comment'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'body-format':
if ($items = field_get_items('comment', $comment, 'comment_body')) {
$format = filter_format_load($items[0]['format']);
$replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
}
break;
}
}
// Chained token relationships.
if ($format_tokens = token_find_with_prefix($tokens, 'body-format')) {
if ($items = field_get_items('comment', $comment, 'comment_body')) {
$body_format = filter_format_load($items[0]['format']);
$replacements += token_generate('format', $format_tokens, array('format' => $body_format), $options);
}
}
}
return $replacements;
}