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