taxonomy.tokens.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
  5. */
  6. use Drupal\Core\Render\BubbleableMetadata;
  7. use Drupal\taxonomy\Entity\Vocabulary;
  8. /**
  9. * Implements hook_token_info().
  10. */
  11. function taxonomy_token_info() {
  12. $types['term'] = [
  13. 'name' => t("Taxonomy terms"),
  14. 'description' => t("Tokens related to taxonomy terms."),
  15. 'needs-data' => 'term',
  16. ];
  17. $types['vocabulary'] = [
  18. 'name' => t("Vocabularies"),
  19. 'description' => t("Tokens related to taxonomy vocabularies."),
  20. 'needs-data' => 'vocabulary',
  21. ];
  22. // Taxonomy term related variables.
  23. $term['tid'] = [
  24. 'name' => t("Term ID"),
  25. 'description' => t("The unique ID of the taxonomy term."),
  26. ];
  27. $term['name'] = [
  28. 'name' => t("Name"),
  29. 'description' => t("The name of the taxonomy term."),
  30. ];
  31. $term['description'] = [
  32. 'name' => t("Description"),
  33. 'description' => t("The optional description of the taxonomy term."),
  34. ];
  35. $term['node-count'] = [
  36. 'name' => t("Node count"),
  37. 'description' => t("The number of nodes tagged with the taxonomy term."),
  38. ];
  39. $term['url'] = [
  40. 'name' => t("URL"),
  41. 'description' => t("The URL of the taxonomy term."),
  42. ];
  43. // Taxonomy vocabulary related variables.
  44. $vocabulary['vid'] = [
  45. 'name' => t("Vocabulary ID"),
  46. 'description' => t("The unique ID of the taxonomy vocabulary."),
  47. ];
  48. $vocabulary['name'] = [
  49. 'name' => t("Name"),
  50. 'description' => t("The name of the taxonomy vocabulary."),
  51. ];
  52. $vocabulary['description'] = [
  53. 'name' => t("Description"),
  54. 'description' => t("The optional description of the taxonomy vocabulary."),
  55. ];
  56. $vocabulary['node-count'] = [
  57. 'name' => t("Node count"),
  58. 'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
  59. ];
  60. $vocabulary['term-count'] = [
  61. 'name' => t("Term count"),
  62. 'description' => t("The number of terms belonging to the taxonomy vocabulary."),
  63. ];
  64. // Chained tokens for taxonomies
  65. $term['vocabulary'] = [
  66. 'name' => t("Vocabulary"),
  67. 'description' => t("The vocabulary the taxonomy term belongs to."),
  68. 'type' => 'vocabulary',
  69. ];
  70. $term['parent'] = [
  71. 'name' => t("Parent term"),
  72. 'description' => t("The parent term of the taxonomy term, if one exists."),
  73. 'type' => 'term',
  74. ];
  75. return [
  76. 'types' => $types,
  77. 'tokens' => [
  78. 'term' => $term,
  79. 'vocabulary' => $vocabulary,
  80. ],
  81. ];
  82. }
  83. /**
  84. * Implements hook_tokens().
  85. */
  86. function taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  87. $token_service = \Drupal::token();
  88. $replacements = [];
  89. $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  90. if ($type == 'term' && !empty($data['term'])) {
  91. $term = $data['term'];
  92. foreach ($tokens as $name => $original) {
  93. switch ($name) {
  94. case 'tid':
  95. $replacements[$original] = $term->id();
  96. break;
  97. case 'name':
  98. $replacements[$original] = $term->getName();
  99. break;
  100. case 'description':
  101. // "processed" returns a \Drupal\Component\Render\MarkupInterface via
  102. // check_markup().
  103. $replacements[$original] = $term->description->processed;
  104. break;
  105. case 'url':
  106. $replacements[$original] = $term->toUrl('canonical', ['absolute' => TRUE])->toString();
  107. break;
  108. case 'node-count':
  109. $query = \Drupal::database()->select('taxonomy_index');
  110. $query->condition('tid', $term->id());
  111. $query->addTag('term_node_count');
  112. $count = $query->countQuery()->execute()->fetchField();
  113. $replacements[$original] = $count;
  114. break;
  115. case 'vocabulary':
  116. $vocabulary = Vocabulary::load($term->bundle());
  117. $bubbleable_metadata->addCacheableDependency($vocabulary);
  118. $replacements[$original] = $vocabulary->label();
  119. break;
  120. case 'parent':
  121. if ($parents = $taxonomy_storage->loadParents($term->id())) {
  122. $parent = array_pop($parents);
  123. $bubbleable_metadata->addCacheableDependency($parent);
  124. $replacements[$original] = $parent->getName();
  125. }
  126. break;
  127. }
  128. }
  129. if ($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'vocabulary')) {
  130. $vocabulary = Vocabulary::load($term->bundle());
  131. $replacements += $token_service->generate('vocabulary', $vocabulary_tokens, ['vocabulary' => $vocabulary], $options, $bubbleable_metadata);
  132. }
  133. if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = $taxonomy_storage->loadParents($term->id())) {
  134. $parent = array_pop($parents);
  135. $replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata);
  136. }
  137. }
  138. elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
  139. $vocabulary = $data['vocabulary'];
  140. foreach ($tokens as $name => $original) {
  141. switch ($name) {
  142. case 'vid':
  143. $replacements[$original] = $vocabulary->id();
  144. break;
  145. case 'name':
  146. $replacements[$original] = $vocabulary->label();
  147. break;
  148. case 'description':
  149. $build = ['#markup' => $vocabulary->getDescription()];
  150. // @todo Fix in https://www.drupal.org/node/2577827
  151. $replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
  152. break;
  153. case 'term-count':
  154. $replacements[$original] = \Drupal::entityQuery('taxonomy_term')
  155. ->condition('vid', $vocabulary->id())
  156. ->addTag('vocabulary_term_count')
  157. ->count()
  158. ->execute();
  159. break;
  160. case 'node-count':
  161. $replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
  162. break;
  163. }
  164. }
  165. }
  166. return $replacements;
  167. }