comment.tokens.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for comment-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function comment_token_info() {
  10. $type = array(
  11. 'name' => t('Comments'),
  12. 'description' => t('Tokens for comments posted on the site.'),
  13. 'needs-data' => 'comment',
  14. );
  15. // Comment-related tokens for nodes
  16. $node['comment-count'] = array(
  17. 'name' => t("Comment count"),
  18. 'description' => t("The number of comments posted on a node."),
  19. );
  20. $node['comment-count-new'] = array(
  21. 'name' => t("New comment count"),
  22. 'description' => t("The number of comments posted on a node since the reader last viewed it."),
  23. );
  24. // Core comment tokens
  25. $comment['cid'] = array(
  26. 'name' => t("Comment ID"),
  27. 'description' => t("The unique ID of the comment."),
  28. );
  29. $comment['hostname'] = array(
  30. 'name' => t("IP Address"),
  31. 'description' => t("The IP address of the computer the comment was posted from."),
  32. );
  33. $comment['name'] = array(
  34. 'name' => t("Name"),
  35. 'description' => t("The name left by the comment author."),
  36. );
  37. $comment['mail'] = array(
  38. 'name' => t("Email address"),
  39. 'description' => t("The email address left by the comment author."),
  40. );
  41. $comment['homepage'] = array(
  42. 'name' => t("Home page"),
  43. 'description' => t("The home page URL left by the comment author."),
  44. );
  45. $comment['title'] = array(
  46. 'name' => t("Title"),
  47. 'description' => t("The title of the comment."),
  48. );
  49. $comment['body'] = array(
  50. 'name' => t("Content"),
  51. 'description' => t("The formatted content of the comment itself."),
  52. );
  53. $comment['url'] = array(
  54. 'name' => t("URL"),
  55. 'description' => t("The URL of the comment."),
  56. );
  57. $comment['edit-url'] = array(
  58. 'name' => t("Edit URL"),
  59. 'description' => t("The URL of the comment's edit page."),
  60. );
  61. // Chained tokens for comments
  62. $comment['created'] = array(
  63. 'name' => t("Date created"),
  64. 'description' => t("The date the comment was posted."),
  65. 'type' => 'date',
  66. );
  67. $comment['changed'] = array(
  68. 'name' => t("Date changed"),
  69. 'description' => t("The date the comment was most recently updated."),
  70. 'type' => 'date',
  71. );
  72. $comment['parent'] = array(
  73. 'name' => t("Parent"),
  74. 'description' => t("The comment's parent, if comment threading is active."),
  75. 'type' => 'comment',
  76. );
  77. $comment['node'] = array(
  78. 'name' => t("Node"),
  79. 'description' => t("The node the comment was posted to."),
  80. 'type' => 'node',
  81. );
  82. $comment['author'] = array(
  83. 'name' => t("Author"),
  84. 'description' => t("The author of the comment, if they were logged in."),
  85. 'type' => 'user',
  86. );
  87. return array(
  88. 'types' => array('comment' => $type),
  89. 'tokens' => array(
  90. 'node' => $node,
  91. 'comment' => $comment,
  92. ),
  93. );
  94. }
  95. /**
  96. * Implements hook_tokens().
  97. */
  98. function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
  99. $url_options = array('absolute' => TRUE);
  100. if (isset($options['language'])) {
  101. $url_options['language'] = $options['language'];
  102. $language_code = $options['language']->language;
  103. }
  104. else {
  105. $language_code = NULL;
  106. }
  107. $sanitize = !empty($options['sanitize']);
  108. $replacements = array();
  109. if ($type == 'comment' && !empty($data['comment'])) {
  110. $comment = $data['comment'];
  111. foreach ($tokens as $name => $original) {
  112. switch ($name) {
  113. // Simple key values on the comment.
  114. case 'cid':
  115. $replacements[$original] = $comment->cid;
  116. break;
  117. // Poster identity information for comments
  118. case 'hostname':
  119. $replacements[$original] = $sanitize ? check_plain($comment->hostname) : $comment->hostname;
  120. break;
  121. case 'name':
  122. $name = ($comment->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $comment->name;
  123. $replacements[$original] = $sanitize ? filter_xss($name) : $name;
  124. break;
  125. case 'mail':
  126. if ($comment->uid != 0) {
  127. $account = user_load($comment->uid);
  128. $mail = $account->mail;
  129. }
  130. else {
  131. $mail = $comment->mail;
  132. }
  133. $replacements[$original] = $sanitize ? check_plain($mail) : $mail;
  134. break;
  135. case 'homepage':
  136. $replacements[$original] = $sanitize ? check_url($comment->homepage) : $comment->homepage;
  137. break;
  138. case 'title':
  139. $replacements[$original] = $sanitize ? filter_xss($comment->subject) : $comment->subject;
  140. break;
  141. case 'body':
  142. if ($items = field_get_items('comment', $comment, 'comment_body', $language_code)) {
  143. $instance = field_info_instance('comment', 'body', 'comment_body');
  144. $field_langcode = field_language('comment', $comment, 'comment_body', $language_code);
  145. $replacements[$original] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
  146. }
  147. break;
  148. // Comment related URLs.
  149. case 'url':
  150. $url_options['fragment'] = 'comment-' . $comment->cid;
  151. $replacements[$original] = url('comment/' . $comment->cid, $url_options);
  152. break;
  153. case 'edit-url':
  154. $url_options['fragment'] = NULL;
  155. $replacements[$original] = url('comment/' . $comment->cid . '/edit', $url_options);
  156. break;
  157. // Default values for the chained tokens handled below.
  158. case 'author':
  159. $replacements[$original] = $sanitize ? filter_xss($comment->name) : $comment->name;
  160. break;
  161. case 'parent':
  162. if (!empty($comment->pid)) {
  163. $parent = comment_load($comment->pid);
  164. $replacements[$original] = $sanitize ? filter_xss($parent->subject) : $parent->subject;
  165. }
  166. break;
  167. case 'created':
  168. $replacements[$original] = format_date($comment->created, 'medium', '', NULL, $language_code);
  169. break;
  170. case 'changed':
  171. $replacements[$original] = format_date($comment->changed, 'medium', '', NULL, $language_code);
  172. break;
  173. case 'node':
  174. $node = node_load($comment->nid);
  175. $title = $node->title;
  176. $replacements[$original] = $sanitize ? filter_xss($title) : $title;
  177. break;
  178. }
  179. }
  180. // Chained token relationships.
  181. if ($node_tokens = token_find_with_prefix($tokens, 'node')) {
  182. $node = node_load($comment->nid);
  183. $replacements += token_generate('node', $node_tokens, array('node' => $node), $options);
  184. }
  185. if ($date_tokens = token_find_with_prefix($tokens, 'created')) {
  186. $replacements += token_generate('date', $date_tokens, array('date' => $comment->created), $options);
  187. }
  188. if ($date_tokens = token_find_with_prefix($tokens, 'changed')) {
  189. $replacements += token_generate('date', $date_tokens, array('date' => $comment->changed), $options);
  190. }
  191. if (($parent_tokens = token_find_with_prefix($tokens, 'parent')) && $parent = comment_load($comment->pid)) {
  192. $replacements += token_generate('comment', $parent_tokens, array('comment' => $parent), $options);
  193. }
  194. if (($author_tokens = token_find_with_prefix($tokens, 'author')) && $account = user_load($comment->uid)) {
  195. $replacements += token_generate('user', $author_tokens, array('user' => $account), $options);
  196. }
  197. }
  198. elseif ($type == 'node' & !empty($data['node'])) {
  199. $node = $data['node'];
  200. foreach ($tokens as $name => $original) {
  201. switch($name) {
  202. case 'comment-count':
  203. $replacements[$original] = $node->comment_count;
  204. break;
  205. case 'comment-count-new':
  206. $replacements[$original] = comment_num_new($node->nid);
  207. break;
  208. }
  209. }
  210. }
  211. return $replacements;
  212. }