flag.tokens.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @file
  4. * Flag module tokens support.
  5. */
  6. /**
  7. * Implements of hook_token_info().
  8. *
  9. * The tokens we provide on generic entities require token module.
  10. */
  11. function flag_token_info() {
  12. $types = array();
  13. $tokens = array();
  14. // Flag tokens.
  15. $types['flag'] = array(
  16. 'name' => t('Flags'),
  17. 'description' => t('Tokens related to flag data.'),
  18. 'needs-data' => 'flag',
  19. );
  20. $tokens['flag']['name'] = array(
  21. 'name' => t('Flag name'),
  22. 'description' => t('The flag machine-readable name.'),
  23. );
  24. $tokens['flag']['title'] = array(
  25. 'name' => t('Flag title'),
  26. 'description' => t('The human-readable flag title.'),
  27. );
  28. // Flagging tokens.
  29. //
  30. // Attached fields are exposed as tokens via some contrib module, but we
  31. // need to expose other fields ourselves. Currently, 'date' is the only such
  32. // field we expose.
  33. $types['flagging'] = array(
  34. 'name' => t('Flaggings'),
  35. 'description' => t('Tokens related to flaggings.'),
  36. 'needs-data' => 'flagging',
  37. );
  38. $tokens['flagging']['date'] = array(
  39. 'name' => t('Flagging date'),
  40. 'description' => t('The date an item was flagged.'),
  41. 'type' => 'date',
  42. );
  43. // Flag action tokens.
  44. $types['flag-action'] = array(
  45. 'name' => t('Flag actions'),
  46. 'description' => t('Tokens available in response to a flag action being executed by a user.'),
  47. 'needs-data' => 'flag-action',
  48. );
  49. $tokens['flag-action']['action'] = array(
  50. 'name' => t('Flag action'),
  51. 'description' => t('The flagging action taking place, either "flag" or "unflag".'),
  52. );
  53. $tokens['flag-action']['entity-url'] = array(
  54. 'name' => t('Flag entity URL'),
  55. 'description' => t('The URL of the entity being flagged.'),
  56. );
  57. $tokens['flag-action']['entity-title'] = array(
  58. 'name' => t('Flag entity title'),
  59. 'description' => t('The title of the entity being flagged.'),
  60. );
  61. $tokens['flag-action']['entity-type'] = array(
  62. 'name' => t('Flag entity type'),
  63. 'description' => t('The type of entity being flagged, such as <em>node</em> or <em>comment</em>.'),
  64. );
  65. $tokens['flag-action']['entity-id'] = array(
  66. 'name' => t('Flag entity ID'),
  67. 'description' => t('The ID of entity being flagged, such as a nid or cid.'),
  68. );
  69. $tokens['flag-action']['count'] = array(
  70. 'name' => t('Flag count'),
  71. 'description' => t('The current count total for this flag.'),
  72. );
  73. // Add tokens for the flag count available at the entity level.
  74. // These require token module because we need its helper data and functions
  75. // to deal with token types that are not the same as the entity types they are
  76. // for (in particular, terms and vocabularies).
  77. if (module_exists('token')) {
  78. $entity_info = entity_get_info();
  79. foreach (flag_get_types() as $flag_type) {
  80. // If the flag type is not an entity type then skip.
  81. if (!isset($entity_info[$flag_type])) {
  82. continue;
  83. }
  84. // The flag type is the entity type, but this is not necessarily the same
  85. // as the entity's token type.
  86. $token_type = $entity_info[$flag_type]['token type'];
  87. $flags = flag_get_flags($flag_type);
  88. foreach ($flags as $flag) {
  89. $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
  90. 'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
  91. 'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
  92. 'flag-type' => $flag_type,
  93. );
  94. $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
  95. 'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
  96. 'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
  97. 'flag-type' => $flag_type,
  98. );
  99. }
  100. }
  101. }
  102. return array(
  103. 'types' => $types,
  104. 'tokens' => $tokens,
  105. );
  106. }
  107. /**
  108. * Implements hook_tokens().
  109. */
  110. function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
  111. $replacements = array();
  112. $sanitize = !empty($options['sanitize']);
  113. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  114. if ($type == 'flag' && !empty($data['flag'])) {
  115. $flag = $data['flag'];
  116. foreach ($tokens as $name => $original) {
  117. switch ($name) {
  118. case 'name':
  119. $replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
  120. break;
  121. case 'title':
  122. $replacements[$original] = $sanitize ? check_plain($flag->get_title()) : $flag->get_title();
  123. break;
  124. }
  125. }
  126. }
  127. elseif ($type == 'flagging' && !empty($data['flagging'])) {
  128. $flagging = $data['flagging'];
  129. foreach ($tokens as $name => $original) {
  130. switch ($name) {
  131. case 'date':
  132. $replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
  133. break;
  134. }
  135. }
  136. if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
  137. $replacements += token_generate('date', $date_tokens, array('date' => $flagging->timestamp), $options);
  138. }
  139. }
  140. elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
  141. $action = $data['flag-action'];
  142. foreach ($tokens as $name => $original) {
  143. switch ($name) {
  144. case 'action':
  145. $replacements[$original] = $action->action;
  146. break;
  147. case 'entity-url':
  148. $replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
  149. break;
  150. case 'entity-title':
  151. $replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
  152. break;
  153. case 'entity-type':
  154. $replacements[$original] = $action->entity_type;
  155. break;
  156. case 'entity-id':
  157. $replacements[$original] = $action->entity_id;
  158. break;
  159. case 'count':
  160. $replacements[$original] = $action->count;
  161. break;
  162. }
  163. }
  164. }
  165. // We only provide tokens on entity types if we have token module's helper
  166. // methods available.
  167. if (isset($data[$type]) && module_exists('token')) {
  168. $entity_type = token_get_entity_mapping('token', $type);
  169. if ($entity_type && in_array($entity_type, flag_get_types())) {
  170. $flags = flag_get_flags($entity_type);
  171. $object = $data[$type];
  172. foreach ($flags as $flag) {
  173. foreach ($tokens as $name => $original) {
  174. $flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
  175. $flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
  176. if ($name == $flag_count_token) {
  177. $replacements[$original] = $flag->get_count($flag->get_entity_id($object));
  178. }
  179. elseif ($name == $flag_link_token) {
  180. $replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return $replacements;
  187. }
  188. /**
  189. * Returns HTML for a tokens browser.
  190. *
  191. * @param array $variables
  192. * An associative array containing:
  193. * - types: An array naming the types of tokens to show.
  194. * - global_types: Whether to show global tokens.
  195. */
  196. function theme_flag_tokens_browser($variables) {
  197. $types = $variables['types'];
  198. $global_types = $variables['global_types'];
  199. if (module_exists('token')) {
  200. return theme('token_tree', array('token_types' => $types, 'global_types' => $global_types, 'dialog' => TRUE));
  201. }
  202. else {
  203. return '<p><em>' . t("Note: You don't have the <a href='@token-url'>Token</a> module installed, so the list of available tokens isn't shown here. You don't have to install <a href='@token-url'>Token</a> to be able to use tokens, but if you have it installed, and enabled, you'll be able to enjoy an interactive tokens browser.", array('@token-url' => 'http://drupal.org/project/token')) . '</em></p>';
  204. }
  205. }