5 * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
8 use Drupal\Core\Render\BubbleableMetadata;
9 use Drupal\taxonomy\Entity\Vocabulary;
12 * Implements hook_token_info().
14 function taxonomy_token_info() {
16 'name' => t("Taxonomy terms"),
17 'description' => t("Tokens related to taxonomy terms."),
18 'needs-data' => 'term',
20 $types['vocabulary'] = [
21 'name' => t("Vocabularies"),
22 'description' => t("Tokens related to taxonomy vocabularies."),
23 'needs-data' => 'vocabulary',
26 // Taxonomy term related variables.
28 'name' => t("Term ID"),
29 'description' => t("The unique ID of the taxonomy term."),
33 'description' => t("The name of the taxonomy term."),
35 $term['description'] = [
36 'name' => t("Description"),
37 'description' => t("The optional description of the taxonomy term."),
39 $term['node-count'] = [
40 'name' => t("Node count"),
41 'description' => t("The number of nodes tagged with the taxonomy term."),
45 'description' => t("The URL of the taxonomy term."),
48 // Taxonomy vocabulary related variables.
49 $vocabulary['vid'] = [
50 'name' => t("Vocabulary ID"),
51 'description' => t("The unique ID of the taxonomy vocabulary."),
53 $vocabulary['name'] = [
55 'description' => t("The name of the taxonomy vocabulary."),
57 $vocabulary['description'] = [
58 'name' => t("Description"),
59 'description' => t("The optional description of the taxonomy vocabulary."),
61 $vocabulary['node-count'] = [
62 'name' => t("Node count"),
63 'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
65 $vocabulary['term-count'] = [
66 'name' => t("Term count"),
67 'description' => t("The number of terms belonging to the taxonomy vocabulary."),
70 // Chained tokens for taxonomies
71 $term['vocabulary'] = [
72 'name' => t("Vocabulary"),
73 'description' => t("The vocabulary the taxonomy term belongs to."),
74 'type' => 'vocabulary',
77 'name' => t("Parent term"),
78 'description' => t("The parent term of the taxonomy term, if one exists."),
86 'vocabulary' => $vocabulary,
92 * Implements hook_tokens().
94 function taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
95 $token_service = \Drupal::token();
98 $taxonomy_storage = \Drupal::entityManager()->getStorage('taxonomy_term');
99 if ($type == 'term' && !empty($data['term'])) {
100 $term = $data['term'];
102 foreach ($tokens as $name => $original) {
105 $replacements[$original] = $term->id();
109 $replacements[$original] = $term->getName();
113 // "processed" returns a \Drupal\Component\Render\MarkupInterface via
115 $replacements[$original] = $term->description->processed;
119 $replacements[$original] = $term->url('canonical', ['absolute' => TRUE]);
123 $query = db_select('taxonomy_index');
124 $query->condition('tid', $term->id());
125 $query->addTag('term_node_count');
126 $count = $query->countQuery()->execute()->fetchField();
127 $replacements[$original] = $count;
131 $vocabulary = Vocabulary::load($term->bundle());
132 $bubbleable_metadata->addCacheableDependency($vocabulary);
133 $replacements[$original] = $vocabulary->label();
137 if ($parents = $taxonomy_storage->loadParents($term->id())) {
138 $parent = array_pop($parents);
139 $bubbleable_metadata->addCacheableDependency($parent);
140 $replacements[$original] = $parent->getName();
146 if ($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'vocabulary')) {
147 $vocabulary = Vocabulary::load($term->bundle());
148 $replacements += $token_service->generate('vocabulary', $vocabulary_tokens, ['vocabulary' => $vocabulary], $options, $bubbleable_metadata);
151 if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = $taxonomy_storage->loadParents($term->id())) {
152 $parent = array_pop($parents);
153 $replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata);
157 elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
158 $vocabulary = $data['vocabulary'];
160 foreach ($tokens as $name => $original) {
163 $replacements[$original] = $vocabulary->id();
167 $replacements[$original] = $vocabulary->label();
171 $build = ['#markup' => $vocabulary->getDescription()];
172 // @todo Fix in https://www.drupal.org/node/2577827
173 $replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
177 $replacements[$original] = \Drupal::entityQuery('taxonomy_term')
178 ->condition('vid', $vocabulary->id())
179 ->addTag('vocabulary_term_count')
185 $replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
191 return $replacements;