node.tokens.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for node-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function node_token_info() {
  10. $type = array(
  11. 'name' => t('Nodes'),
  12. 'description' => t('Tokens related to individual content items, or "nodes".'),
  13. 'needs-data' => 'node',
  14. );
  15. // Core tokens for nodes.
  16. $node['nid'] = array(
  17. 'name' => t("Content ID"),
  18. 'description' => t('The unique ID of the content item, or "node".'),
  19. );
  20. $node['vid'] = array(
  21. 'name' => t("Revision ID"),
  22. 'description' => t("The unique ID of the node's latest revision."),
  23. );
  24. $node['tnid'] = array(
  25. 'name' => t("Translation set ID"),
  26. 'description' => t("The unique ID of the original-language version of this node, if one exists."),
  27. );
  28. $node['type'] = array(
  29. 'name' => t("Content type"),
  30. 'description' => t("The type of the node."),
  31. );
  32. $node['type-name'] = array(
  33. 'name' => t("Content type name"),
  34. 'description' => t("The human-readable name of the node type."),
  35. );
  36. $node['title'] = array(
  37. 'name' => t("Title"),
  38. 'description' => t("The title of the node."),
  39. );
  40. $node['body'] = array(
  41. 'name' => t("Body"),
  42. 'description' => t("The main body text of the node."),
  43. );
  44. $node['summary'] = array(
  45. 'name' => t("Summary"),
  46. 'description' => t("The summary of the node's main body text."),
  47. );
  48. $node['language'] = array(
  49. 'name' => t("Language"),
  50. 'description' => t("The language the node is written in."),
  51. );
  52. $node['url'] = array(
  53. 'name' => t("URL"),
  54. 'description' => t("The URL of the node."),
  55. );
  56. $node['edit-url'] = array(
  57. 'name' => t("Edit URL"),
  58. 'description' => t("The URL of the node's edit page."),
  59. );
  60. // Chained tokens for nodes.
  61. $node['created'] = array(
  62. 'name' => t("Date created"),
  63. 'description' => t("The date the node was posted."),
  64. 'type' => 'date',
  65. );
  66. $node['changed'] = array(
  67. 'name' => t("Date changed"),
  68. 'description' => t("The date the node was most recently updated."),
  69. 'type' => 'date',
  70. );
  71. $node['author'] = array(
  72. 'name' => t("Author"),
  73. 'description' => t("The author of the node."),
  74. 'type' => 'user',
  75. );
  76. return array(
  77. 'types' => array('node' => $type),
  78. 'tokens' => array('node' => $node),
  79. );
  80. }
  81. /**
  82. * Implements hook_tokens().
  83. */
  84. function node_tokens($type, $tokens, array $data = array(), array $options = array()) {
  85. $url_options = array('absolute' => TRUE);
  86. if (isset($options['language'])) {
  87. $url_options['language'] = $options['language'];
  88. $language_code = $options['language']->language;
  89. }
  90. else {
  91. $language_code = NULL;
  92. }
  93. $sanitize = !empty($options['sanitize']);
  94. $replacements = array();
  95. if ($type == 'node' && !empty($data['node'])) {
  96. $node = $data['node'];
  97. foreach ($tokens as $name => $original) {
  98. switch ($name) {
  99. // Simple key values on the node.
  100. case 'nid':
  101. $replacements[$original] = $node->nid;
  102. break;
  103. case 'vid':
  104. $replacements[$original] = $node->vid;
  105. break;
  106. case 'tnid':
  107. $replacements[$original] = $node->tnid;
  108. break;
  109. case 'type':
  110. $replacements[$original] = $sanitize ? check_plain($node->type) : $node->type;
  111. break;
  112. case 'type-name':
  113. $type_name = node_type_get_name($node);
  114. $replacements[$original] = $sanitize ? check_plain($type_name) : $type_name;
  115. break;
  116. case 'title':
  117. $replacements[$original] = $sanitize ? check_plain($node->title) : $node->title;
  118. break;
  119. case 'body':
  120. case 'summary':
  121. if ($items = field_get_items('node', $node, 'body', $language_code)) {
  122. $instance = field_info_instance('node', 'body', $node->type);
  123. $field_langcode = field_language('node', $node, 'body', $language_code);
  124. // If the summary was requested and is not empty, use it.
  125. if ($name == 'summary' && !empty($items[0]['summary'])) {
  126. $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary'];
  127. }
  128. // Attempt to provide a suitable version of the 'body' field.
  129. else {
  130. $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
  131. // A summary was requested.
  132. if ($name == 'summary') {
  133. if (isset($instance['display']['teaser']['settings']['trim_length'])) {
  134. $trim_length = $instance['display']['teaser']['settings']['trim_length'];
  135. }
  136. else {
  137. // Use default value.
  138. $trim_length = NULL;
  139. }
  140. // Generate an optionally trimmed summary of the body field.
  141. $output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $trim_length);
  142. }
  143. }
  144. $replacements[$original] = $output;
  145. }
  146. break;
  147. case 'language':
  148. $langcode = entity_language('node', $node);
  149. $replacements[$original] = $sanitize ? check_plain($langcode) : $langcode;
  150. break;
  151. case 'url':
  152. $replacements[$original] = url('node/' . $node->nid, $url_options);
  153. break;
  154. case 'edit-url':
  155. $replacements[$original] = url('node/' . $node->nid . '/edit', $url_options);
  156. break;
  157. // Default values for the chained tokens handled below.
  158. case 'author':
  159. $account = user_load($node->uid);
  160. $name = format_username($account);
  161. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  162. break;
  163. case 'created':
  164. $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
  165. break;
  166. case 'changed':
  167. $replacements[$original] = format_date($node->changed, 'medium', '', NULL, $language_code);
  168. break;
  169. }
  170. }
  171. if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
  172. $author = user_load($node->uid);
  173. $replacements += token_generate('user', $author_tokens, array('user' => $author), $options);
  174. }
  175. if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
  176. $replacements += token_generate('date', $created_tokens, array('date' => $node->created), $options);
  177. }
  178. if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
  179. $replacements += token_generate('date', $changed_tokens, array('date' => $node->changed), $options);
  180. }
  181. }
  182. return $replacements;
  183. }