flag.tokens.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // The flag type is the entity type, but this is not necessarily the same
  81. // as the entity's token type.
  82. $token_type = $entity_info[$flag_type]['token type'];
  83. $flags = flag_get_flags($flag_type);
  84. foreach ($flags as $flag) {
  85. $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
  86. 'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
  87. 'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
  88. 'flag-type' => $flag_type,
  89. );
  90. $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
  91. 'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
  92. 'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
  93. 'flag-type' => $flag_type,
  94. );
  95. }
  96. }
  97. }
  98. return array(
  99. 'types' => $types,
  100. 'tokens' => $tokens,
  101. );
  102. }
  103. /**
  104. * Implements hook_tokens().
  105. */
  106. function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
  107. $replacements = array();
  108. $sanitize = !empty($options['sanitize']);
  109. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  110. if ($type == 'flag' && !empty($data['flag'])) {
  111. $flag = $data['flag'];
  112. foreach ($tokens as $name => $original) {
  113. switch ($name) {
  114. case 'name':
  115. $replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
  116. break;
  117. case 'title':
  118. $replacements[$original] = $sanitize ? check_plain($flag->get_title()) : $flag->get_title();
  119. break;
  120. }
  121. }
  122. }
  123. elseif ($type == 'flagging' && !empty($data['flagging'])) {
  124. $flagging = $data['flagging'];
  125. foreach ($tokens as $name => $original) {
  126. switch ($name) {
  127. case 'date':
  128. $replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
  129. break;
  130. }
  131. }
  132. if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
  133. $replacements += token_generate('date', $date_tokens, array('date' => $flagging->timestamp), $options);
  134. }
  135. }
  136. elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
  137. $action = $data['flag-action'];
  138. foreach ($tokens as $name => $original) {
  139. switch ($name) {
  140. case 'action':
  141. $replacements[$original] = $action->action;
  142. break;
  143. case 'entity-url':
  144. $replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
  145. break;
  146. case 'entity-title':
  147. $replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
  148. break;
  149. case 'entity-type':
  150. $replacements[$original] = $action->entity_type;
  151. break;
  152. case 'entity-id':
  153. $replacements[$original] = $action->entity_id;
  154. break;
  155. case 'count':
  156. $replacements[$original] = $action->count;
  157. break;
  158. }
  159. }
  160. }
  161. // We only provide tokens on entity types if we have token module's helper
  162. // methods available.
  163. if (isset($data[$type]) && module_exists('token')) {
  164. $entity_type = token_get_entity_mapping('token', $type);
  165. if ($entity_type && in_array($entity_type, flag_get_types())) {
  166. $flags = flag_get_flags($entity_type);
  167. $object = $data[$type];
  168. foreach ($flags as $flag) {
  169. foreach ($tokens as $name => $original) {
  170. $flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
  171. $flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
  172. if ($name == $flag_count_token) {
  173. $replacements[$original] = $flag->get_count($flag->get_entity_id($object));
  174. }
  175. elseif ($name == $flag_link_token) {
  176. $replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
  177. }
  178. }
  179. }
  180. }
  181. }
  182. return $replacements;
  183. }
  184. /**
  185. * Returns HTML for a tokens browser.
  186. *
  187. * @param $variables
  188. * An associative array containing:
  189. * - types: An array naming the types of tokens to show.
  190. * - global_types: Whether to show global tokens.
  191. */
  192. function theme_flag_tokens_browser($variables) {
  193. $types = $variables['types'];
  194. $global_types = $variables['global_types'];
  195. if (module_exists('token')) {
  196. return theme('token_tree', array('token_types' => $types, 'global_types' => $global_types));
  197. }
  198. else {
  199. 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>';
  200. }
  201. }