node.tokens.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. $column = ($name == 'body') ? 'value' : 'summary';
  123. $instance = field_info_instance('node', 'body', $node->type);
  124. $field_langcode = field_language('node', $node, 'body', $language_code);
  125. $replacements[$original] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], $column) : $items[0][$column];
  126. }
  127. break;
  128. case 'language':
  129. $langcode = entity_language('node', $node);
  130. $replacements[$original] = $sanitize ? check_plain($langcode) : $langcode;
  131. break;
  132. case 'url':
  133. $replacements[$original] = url('node/' . $node->nid, $url_options);
  134. break;
  135. case 'edit-url':
  136. $replacements[$original] = url('node/' . $node->nid . '/edit', $url_options);
  137. break;
  138. // Default values for the chained tokens handled below.
  139. case 'author':
  140. $account = user_load($node->uid);
  141. $name = format_username($account);
  142. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  143. break;
  144. case 'created':
  145. $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
  146. break;
  147. case 'changed':
  148. $replacements[$original] = format_date($node->changed, 'medium', '', NULL, $language_code);
  149. break;
  150. }
  151. }
  152. if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
  153. $author = user_load($node->uid);
  154. $replacements += token_generate('user', $author_tokens, array('user' => $author), $options);
  155. }
  156. if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
  157. $replacements += token_generate('date', $created_tokens, array('date' => $node->created), $options);
  158. }
  159. if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
  160. $replacements += token_generate('date', $changed_tokens, array('date' => $node->changed), $options);
  161. }
  162. }
  163. return $replacements;
  164. }