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