taxonomy.tokens.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function taxonomy_token_info() {
  10. $types['term'] = array(
  11. 'name' => t("Taxonomy terms"),
  12. 'description' => t("Tokens related to taxonomy terms."),
  13. 'needs-data' => 'term',
  14. );
  15. $types['vocabulary'] = array(
  16. 'name' => t("Vocabularies"),
  17. 'description' => t("Tokens related to taxonomy vocabularies."),
  18. 'needs-data' => 'vocabulary',
  19. );
  20. // Taxonomy term related variables.
  21. $term['tid'] = array(
  22. 'name' => t("Term ID"),
  23. 'description' => t("The unique ID of the taxonomy term."),
  24. );
  25. $term['name'] = array(
  26. 'name' => t("Name"),
  27. 'description' => t("The name of the taxonomy term."),
  28. );
  29. $term['description'] = array(
  30. 'name' => t("Description"),
  31. 'description' => t("The optional description of the taxonomy term."),
  32. );
  33. $term['node-count'] = array(
  34. 'name' => t("Node count"),
  35. 'description' => t("The number of nodes tagged with the taxonomy term."),
  36. );
  37. $term['url'] = array(
  38. 'name' => t("URL"),
  39. 'description' => t("The URL of the taxonomy term."),
  40. );
  41. // Taxonomy vocabulary related variables.
  42. $vocabulary['vid'] = array(
  43. 'name' => t("Vocabulary ID"),
  44. 'description' => t("The unique ID of the taxonomy vocabulary."),
  45. );
  46. $vocabulary['name'] = array(
  47. 'name' => t("Name"),
  48. 'description' => t("The name of the taxonomy vocabulary."),
  49. );
  50. $vocabulary['description'] = array(
  51. 'name' => t("Description"),
  52. 'description' => t("The optional description of the taxonomy vocabulary."),
  53. );
  54. $vocabulary['node-count'] = array(
  55. 'name' => t("Node count"),
  56. 'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
  57. );
  58. $vocabulary['term-count'] = array(
  59. 'name' => t("Term count"),
  60. 'description' => t("The number of terms belonging to the taxonomy vocabulary."),
  61. );
  62. // Chained tokens for taxonomies
  63. $term['vocabulary'] = array(
  64. 'name' => t("Vocabulary"),
  65. 'description' => t("The vocabulary the taxonomy term belongs to."),
  66. 'type' => 'vocabulary',
  67. );
  68. $term['parent'] = array(
  69. 'name' => t("Parent term"),
  70. 'description' => t("The parent term of the taxonomy term, if one exists."),
  71. 'type' => 'term',
  72. );
  73. return array(
  74. 'types' => $types,
  75. 'tokens' => array(
  76. 'term' => $term,
  77. 'vocabulary' => $vocabulary,
  78. ),
  79. );
  80. }
  81. /**
  82. * Implements hook_tokens().
  83. */
  84. function taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
  85. $replacements = array();
  86. $sanitize = !empty($options['sanitize']);
  87. if ($type == 'term' && !empty($data['term'])) {
  88. $term = $data['term'];
  89. foreach ($tokens as $name => $original) {
  90. switch ($name) {
  91. case 'tid':
  92. $replacements[$original] = $term->tid;
  93. break;
  94. case 'name':
  95. $replacements[$original] = $sanitize ? check_plain($term->name) : $term->name;
  96. break;
  97. case 'description':
  98. $replacements[$original] = $sanitize ? check_markup($term->description, $term->format, '', TRUE) : $term->description;
  99. break;
  100. case 'url':
  101. $uri = entity_uri('taxonomy_term', $term);
  102. $replacements[$original] = url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE)));
  103. break;
  104. case 'node-count':
  105. $query = db_select('taxonomy_index');
  106. $query->condition('tid', $term->tid);
  107. $query->addTag('term_node_count');
  108. $count = $query->countQuery()->execute()->fetchField();
  109. $replacements[$original] = $count;
  110. break;
  111. case 'vocabulary':
  112. $vocabulary = taxonomy_vocabulary_load($term->vid);
  113. $replacements[$original] = check_plain($vocabulary->name);
  114. break;
  115. case 'parent':
  116. if ($parents = taxonomy_get_parents($term->tid)) {
  117. $parent = array_pop($parents);
  118. $replacements[$original] = check_plain($parent->name);
  119. }
  120. break;
  121. }
  122. }
  123. if ($vocabulary_tokens = token_find_with_prefix($tokens, 'vocabulary')) {
  124. $vocabulary = taxonomy_vocabulary_load($term->vid);
  125. $replacements += token_generate('vocabulary', $vocabulary_tokens, array('vocabulary' => $vocabulary), $options);
  126. }
  127. if (($vocabulary_tokens = token_find_with_prefix($tokens, 'parent')) && $parents = taxonomy_get_parents($term->tid)) {
  128. $parent = array_pop($parents);
  129. $replacements += token_generate('term', $vocabulary_tokens, array('term' => $parent), $options);
  130. }
  131. }
  132. elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
  133. $vocabulary = $data['vocabulary'];
  134. foreach ($tokens as $name => $original) {
  135. switch ($name) {
  136. case 'vid':
  137. $replacements[$original] = $vocabulary->vid;
  138. break;
  139. case 'name':
  140. $replacements[$original] = $sanitize ? check_plain($vocabulary->name) : $vocabulary->name;
  141. break;
  142. case 'description':
  143. $replacements[$original] = $sanitize ? filter_xss($vocabulary->description) : $vocabulary->description;
  144. break;
  145. case 'term-count':
  146. $query = db_select('taxonomy_term_data');
  147. $query->condition('vid', $vocabulary->vid);
  148. $query->addTag('vocabulary_term_count');
  149. $count = $query->countQuery()->execute()->fetchField();
  150. $replacements[$original] = $count;
  151. break;
  152. case 'node-count':
  153. $query = db_select('taxonomy_index', 'ti');
  154. $query->addExpression('COUNT(DISTINCT ti.nid)');
  155. $query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
  156. $query->condition('td.vid', $vocabulary->vid);
  157. $query->addTag('vocabulary_node_count');
  158. $count = $query->execute()->fetchField();
  159. $replacements[$original] = $count;
  160. break;
  161. }
  162. }
  163. }
  164. return $replacements;
  165. }