Newer
Older
<?php
/**
* @file
* Token callbacks for the token_example module.
*/
/**
* Implements hook_token_info().
*
* @ingroup token_example
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
*/
function token_example_token_info() {
// Add two different token types. The first is the generic text format. The
// second is the user's default text format, which is itself a 'format' token
// type so it can be used directly.
$info['types']['format'] = array(
'name' => t('Text formats'),
'description' => t('Tokens related to text formats.'),
'needs-data' => 'format',
);
$info['types']['default-format'] = array(
'name' => t('Default text format'),
'description' => t("Tokens related to the currently logged in user's default text format."),
'type' => 'format',
);
// Tokens for the text format token type.
$info['tokens']['format']['id'] = array(
'name' => t('ID'),
'description' => t("The unique ID of the text format."),
);
$info['tokens']['format']['name'] = array(
'name' => t('Name'),
'description' => t("The name of the text format."),
);
// Node tokens.
$info['tokens']['node']['body-format'] = array(
'name' => t('Body text format'),
'description' => t("The name of the text format used on the node's body field."),
'type' => 'format',
);
// Comment tokens.
if (module_exists('comment')) {
$info['tokens']['comment']['body-format'] = array(
'name' => t('Body text format'),
'description' => t("The name of the text format used on the comment's body field."),
'type' => 'format',
);
}
return $info;
}
/**
* Implements hook_tokens().
*
* @ingroup token_example
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
*/
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, santize 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':
Dave Reid
committed
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')) {
Dave Reid
committed
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':
Dave Reid
committed
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')) {
Dave Reid
committed
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);
}