token_example.module 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @file
  4. * An example module showing how to define and use tokens.
  5. *
  6. * The Token module provides an API for providing tokens to other modules.
  7. * Tokens are small bits of text that can be placed into larger documents
  8. * via simple placeholders, like %site-name or [user].
  9. */
  10. /**
  11. * @defgroup token_example Example: Token API
  12. * @ingroup examples
  13. * @{
  14. * Examples using the Token API.
  15. *
  16. * The Token module provides an API for providing tokens to other modules.
  17. * Tokens are small bits of text that can be placed into larger documents
  18. * via simple placeholders, like %site-name or [user].
  19. *
  20. * Drupal 7 documentation on Token can be found here:
  21. * http://drupal.org/documentation/modules/token
  22. *
  23. * A list of existing tokens can be found here:
  24. * http://drupal.org/node/390482#drupal7tokenslist
  25. *
  26. * This example is part of the Examples for Developers Project which you can
  27. * download and experiment with here: http://drupal.org/project/examples
  28. */
  29. /**
  30. * Implements hook_menu().
  31. */
  32. function token_example_menu() {
  33. $items['examples/token'] = array(
  34. 'title' => 'Token example',
  35. 'description' => 'Test replacement tokens in real time.',
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('token_example_example_form'),
  38. 'access callback' => TRUE,
  39. );
  40. return $items;
  41. }
  42. /**
  43. * Implements hook_entity_info_alter().
  44. *
  45. * @todo Remove this when the testbot can properly pick up dependencies
  46. * for contrib modules.
  47. */
  48. function token_example_entity_info_alter(&$info) {
  49. if (isset($info['taxonomy_term'])) {
  50. $info['taxonomy_term']['token type'] = 'term';
  51. }
  52. if (isset($info['taxonomy_vocabulary'])) {
  53. $info['taxonomy_vocabulary']['token type'] = 'vocabulary';
  54. }
  55. }
  56. /**
  57. * Form builder; display lists of supported token entities and text to tokenize.
  58. */
  59. function token_example_example_form($form, &$form_state) {
  60. $entities = entity_get_info();
  61. $token_types = array();
  62. // Scan through the list of entities for supported token entities.
  63. foreach ($entities as $entity => $info) {
  64. $object_callback = "_token_example_get_{$entity}";
  65. if (function_exists($object_callback) && $objects = $object_callback()) {
  66. $form[$entity] = array(
  67. '#type' => 'select',
  68. '#title' => $info['label'],
  69. '#options' => array(0 => t('Not selected')) + $objects,
  70. '#default_value' => isset($form_state['storage'][$entity]) ? $form_state['storage'][$entity] : 0,
  71. '#access' => !empty($objects),
  72. );
  73. // Build a list of supported token types based on the available entites.
  74. if ($form[$entity]['#access']) {
  75. $token_types[$entity] = !empty($info['token type']) ? $info['token type'] : $entity;
  76. }
  77. }
  78. }
  79. $form['text'] = array(
  80. '#type' => 'textarea',
  81. '#title' => t('Enter your text here'),
  82. '#default_value' => 'Hello [current-user:name]!',
  83. );
  84. // Display the results of tokenized text.
  85. if (!empty($form_state['storage']['text'])) {
  86. $form['text']['#default_value'] = $form_state['storage']['text'];
  87. $data = array();
  88. foreach ($entities as $entity => $info) {
  89. if (!empty($form_state['storage'][$entity])) {
  90. $objects = entity_load($entity, array($form_state['storage'][$entity]));
  91. if ($objects) {
  92. $data[$token_types[$entity]] = reset($objects);
  93. }
  94. }
  95. }
  96. // Display the tokenized text.
  97. $form['text_tokenized'] = array(
  98. '#type' => 'item',
  99. '#title' => t('Result'),
  100. '#markup' => token_replace($form_state['storage']['text'], $data),
  101. );
  102. }
  103. $form['submit'] = array(
  104. '#type' => 'submit',
  105. '#value' => t('Submit'),
  106. );
  107. if (module_exists('token')) {
  108. $form['token_tree'] = array(
  109. '#theme' => 'token_tree',
  110. '#token_types' => $token_types,
  111. );
  112. }
  113. else {
  114. $form['token_tree'] = array(
  115. '#markup' => '<p>' . t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', array('@drupal-token' => 'http://drupal.org/project/token')) . '</p>',
  116. );
  117. }
  118. return $form;
  119. }
  120. /**
  121. * Submit callback; store the submitted values into storage.
  122. */
  123. function token_example_example_form_submit($form, &$form_state) {
  124. $form_state['storage'] = $form_state['values'];
  125. $form_state['rebuild'] = TRUE;
  126. }
  127. /**
  128. * Builds a list of available content.
  129. */
  130. function _token_example_get_node() {
  131. if (!user_access('access content') && !user_access('bypass node access')) {
  132. return array();
  133. }
  134. $node_query = db_select('node', 'n');
  135. $node_query->fields('n', array('nid', 'title'));
  136. $node_query->condition('n.status', NODE_PUBLISHED);
  137. $node_query->orderBy('n.created', 'DESC');
  138. $node_query->range(0, 10);
  139. $node_query->addTag('node_access');
  140. $nodes = $node_query->execute()->fetchAllKeyed();
  141. $nodes = array_map('check_plain', $nodes);
  142. return $nodes;
  143. }
  144. /**
  145. * Builds a list of available comments.
  146. */
  147. function _token_example_get_comment() {
  148. if (!module_exists('comment') || (!user_access('access comments') && !user_access('administer comments'))) {
  149. return array();
  150. }
  151. $comment_query = db_select('comment', 'c');
  152. $comment_query->innerJoin('node', 'n', 'n.nid = c.nid');
  153. $comment_query->fields('c', array('cid', 'subject'));
  154. $comment_query->condition('n.status', NODE_PUBLISHED);
  155. $comment_query->condition('c.status', COMMENT_PUBLISHED);
  156. $comment_query->orderBy('c.created', 'DESC');
  157. $comment_query->range(0, 10);
  158. $comment_query->addTag('node_access');
  159. $comments = $comment_query->execute()->fetchAllKeyed();
  160. $comments = array_map('check_plain', $comments);
  161. return $comments;
  162. }
  163. /**
  164. * Builds a list of available user accounts.
  165. */
  166. function _token_example_get_user() {
  167. if (!user_access('access user profiles') &&
  168. !user_access('administer users')) {
  169. return array();
  170. }
  171. $account_query = db_select('users', 'u');
  172. $account_query->fields('u', array('uid', 'name'));
  173. $account_query->condition('u.uid', 0, '>');
  174. $account_query->condition('u.status', 1);
  175. $account_query->range(0, 10);
  176. $accounts = $account_query->execute()->fetchAllKeyed();
  177. $accounts = array_map('check_plain', $accounts);
  178. return $accounts;
  179. }
  180. /**
  181. * Builds a list of available taxonomy terms.
  182. */
  183. function _token_example_get_taxonomy_term() {
  184. $term_query = db_select('taxonomy_term_data', 'ttd');
  185. $term_query->fields('ttd', array('tid', 'name'));
  186. $term_query->range(0, 10);
  187. $term_query->addTag('term_access');
  188. $terms = $term_query->execute()->fetchAllKeyed();
  189. $terms = array_map('check_plain', $terms);
  190. return $terms;
  191. }
  192. /**
  193. * Builds a list of available files.
  194. */
  195. function _token_example_get_file() {
  196. $file_query = db_select('file_managed', 'f');
  197. $file_query->fields('f', array('fid', 'filename'));
  198. $file_query->range(0, 10);
  199. $files = $file_query->execute()->fetchAllKeyed();
  200. $files = array_map('check_plain', $files);
  201. return $files;
  202. }
  203. /**
  204. * @} End of "defgroup token_example".
  205. */