5 * Builds placeholder replacement tokens for comment-related data.
9 * Implements hook_token_info().
11 function comment_token_info() {
13 'name' => t('Comments'),
14 'description' => t('Tokens for comments posted on the site.'),
15 'needs-data' => 'comment',
18 // Comment-related tokens for nodes
19 $node['comment-count'] = array(
20 'name' => t("Comment count"),
21 'description' => t("The number of comments posted on a node."),
23 $node['comment-count-new'] = array(
24 'name' => t("New comment count"),
25 'description' => t("The number of comments posted on a node since the reader last viewed it."),
28 // Core comment tokens
29 $comment['cid'] = array(
30 'name' => t("Comment ID"),
31 'description' => t("The unique ID of the comment."),
33 $comment['hostname'] = array(
34 'name' => t("IP Address"),
35 'description' => t("The IP address of the computer the comment was posted from."),
37 $comment['name'] = array(
39 'description' => t("The name left by the comment author."),
41 $comment['mail'] = array(
42 'name' => t("Email address"),
43 'description' => t("The email address left by the comment author."),
45 $comment['homepage'] = array(
46 'name' => t("Home page"),
47 'description' => t("The home page URL left by the comment author."),
49 $comment['title'] = array(
51 'description' => t("The title of the comment."),
53 $comment['body'] = array(
54 'name' => t("Content"),
55 'description' => t("The formatted content of the comment itself."),
57 $comment['url'] = array(
59 'description' => t("The URL of the comment."),
61 $comment['edit-url'] = array(
62 'name' => t("Edit URL"),
63 'description' => t("The URL of the comment's edit page."),
66 // Chained tokens for comments
67 $comment['created'] = array(
68 'name' => t("Date created"),
69 'description' => t("The date the comment was posted."),
72 $comment['changed'] = array(
73 'name' => t("Date changed"),
74 'description' => t("The date the comment was most recently updated."),
77 $comment['parent'] = array(
78 'name' => t("Parent"),
79 'description' => t("The comment's parent, if comment threading is active."),
82 $comment['node'] = array(
84 'description' => t("The node the comment was posted to."),
87 $comment['author'] = array(
88 'name' => t("Author"),
89 'description' => t("The author of the comment, if they were logged in."),
94 'types' => array('comment' => $type),
97 'comment' => $comment,
103 * Implements hook_tokens().
105 function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
106 $url_options = array('absolute' => TRUE);
107 if (isset($options['language'])) {
108 $url_options['language'] = $options['language'];
109 $language_code = $options['language']->language;
112 $language_code = NULL;
114 $sanitize = !empty($options['sanitize']);
116 $replacements = array();
118 if ($type == 'comment' && !empty($data['comment'])) {
119 $comment = $data['comment'];
121 foreach ($tokens as $name => $original) {
123 // Simple key values on the comment.
125 $replacements[$original] = $comment->cid;
128 // Poster identity information for comments
130 $replacements[$original] = $sanitize ? check_plain($comment->hostname) : $comment->hostname;
134 $name = ($comment->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $comment->name;
135 $replacements[$original] = $sanitize ? filter_xss($name) : $name;
139 if ($comment->uid != 0) {
140 $account = user_load($comment->uid);
141 $mail = $account->mail;
144 $mail = $comment->mail;
146 $replacements[$original] = $sanitize ? check_plain($mail) : $mail;
150 $replacements[$original] = $sanitize ? check_url($comment->homepage) : $comment->homepage;
154 $replacements[$original] = $sanitize ? filter_xss($comment->subject) : $comment->subject;
158 if ($items = field_get_items('comment', $comment, 'comment_body', $language_code)) {
159 $instance = field_info_instance('comment', 'body', 'comment_body');
160 $field_langcode = field_language('comment', $comment, 'comment_body', $language_code);
161 $replacements[$original] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
165 // Comment related URLs.
167 $url_options['fragment'] = 'comment-' . $comment->cid;
168 $replacements[$original] = url('comment/' . $comment->cid, $url_options);
172 $url_options['fragment'] = NULL;
173 $replacements[$original] = url('comment/' . $comment->cid . '/edit', $url_options);
176 // Default values for the chained tokens handled below.
178 $replacements[$original] = $sanitize ? filter_xss($comment->name) : $comment->name;
182 if (!empty($comment->pid)) {
183 $parent = comment_load($comment->pid);
184 $replacements[$original] = $sanitize ? filter_xss($parent->subject) : $parent->subject;
189 $replacements[$original] = format_date($comment->created, 'medium', '', NULL, $language_code);
193 $replacements[$original] = format_date($comment->changed, 'medium', '', NULL, $language_code);
197 $node = node_load($comment->nid);
198 $title = $node->title;
199 $replacements[$original] = $sanitize ? filter_xss($title) : $title;
204 // Chained token relationships.
205 if ($node_tokens = token_find_with_prefix($tokens, 'node')) {
206 $node = node_load($comment->nid);
207 $replacements += token_generate('node', $node_tokens, array('node' => $node), $options);
210 if ($date_tokens = token_find_with_prefix($tokens, 'created')) {
211 $replacements += token_generate('date', $date_tokens, array('date' => $comment->created), $options);
214 if ($date_tokens = token_find_with_prefix($tokens, 'changed')) {
215 $replacements += token_generate('date', $date_tokens, array('date' => $comment->changed), $options);
218 if (($parent_tokens = token_find_with_prefix($tokens, 'parent')) && $parent = comment_load($comment->pid)) {
219 $replacements += token_generate('comment', $parent_tokens, array('comment' => $parent), $options);
222 if (($author_tokens = token_find_with_prefix($tokens, 'author')) && $account = user_load($comment->uid)) {
223 $replacements += token_generate('user', $author_tokens, array('user' => $account), $options);
226 elseif ($type == 'node' & !empty($data['node'])) {
227 $node = $data['node'];
229 foreach ($tokens as $name => $original) {
231 case 'comment-count':
232 $replacements[$original] = $node->comment_count;
235 case 'comment-count-new':
236 $replacements[$original] = comment_num_new($node->nid);
242 return $replacements;