Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / token.install
1 <?php
2
3 /**
4  * @file
5  * Install, update and uninstall functions for the token module.
6  */
7
8 /**
9  * Implements hook_requirements().
10  */
11 function token_requirements($phase = 'runtime') {
12   $requirements = [];
13
14   if ($phase == 'runtime') {
15     // Check for various token definition problems.
16     $token_problems = token_get_token_problems();
17     // Format and display each token problem.
18     foreach ($token_problems as $problem_key => $problem) {
19       if (!empty($problem['problems'])) {
20         $problems = array_unique($problem['problems']);
21
22         $build = [
23           '#theme' => 'item_list',
24           '#items' => $problems,
25         ];
26
27         $requirements['token-' . $problem_key] = [
28           'title' => $problem['label'],
29           'value' => \Drupal::service('renderer')->renderPlain($build),
30           'severity' => $problem['severity'],
31         ];
32       }
33     }
34   }
35
36   return $requirements;
37 }
38
39 /**
40  * Implements hook_install().
41  */
42 function token_install() {
43   // Create a token view mode for each entity type.
44   $info = \Drupal::entityTypeManager()->getDefinitions();
45   foreach ($info as $entity_type => $entity_type_info) {
46     // We're only interested in entity types with a view builder.
47     if (!$entity_type_info->getViewBuilderClass()) {
48       continue;
49     }
50     // Try to find a token view mode for that entity type.
51     $storage = \Drupal::entityTypeManager()->getStorage('entity_view_mode');
52     // Add a token view mode if it does not already exist.
53     if (!$storage->load("$entity_type.token")) {
54       $storage->create([
55         'targetEntityType' => $entity_type,
56         'id' => "$entity_type.token",
57         'status' => TRUE,
58         'label' => t('Token'),
59       ])->save();
60     }
61   }
62 }
63
64 /**
65  * Build a list of Drupal 6 tokens and their Drupal 7 token names.
66  */
67 function _token_upgrade_token_list() {
68   $tokens = [
69     // Global tokens
70     'user-name' => 'current-user:name',
71     'user-id' => 'current-user:id',
72     'user-mail' => 'current-user:mail',
73     'site-url' => 'site:url',
74     'site-name' => 'site:name',
75     'site-slogan' => 'site:slogan',
76     'site-mission' => 'site:mission',
77     'site-mail' => 'site:mail',
78     'site-date' => 'date:short',
79     //'site-date-' => '', // Date tokens expanded below
80     'current-page-path' => 'current-page:path',
81     'current-page-url' => 'current-page:url',
82     'page-number' => 'current-page:page-number',
83
84     // Comment tokens
85     'comment-cid' => 'comment:cid',
86     'comment-nid' => 'comment:node:nid',
87     'comment-title' => 'comment:title',
88     'comment-body' => 'comment:body',
89     'comment-author-name' => 'comment:author:name',
90     'comment-author-mail' => 'comment:author:mail',
91     //'comment-body-format' => '',
92     //'comment-' => '', // Date tokens expanded below
93     'comment-node-title' => 'comment:node',
94
95     // Node tokens
96     'nid' => 'node:nid',
97     'type' => 'node:type',
98     'type-name' => 'node:type-name',
99     'language' => 'node:language',
100     'title' => 'node:title',
101     'author-uid' => 'node:author:uid',
102     'author-name' => 'node:author:name',
103     'author-mail' => 'node:author:mail',
104     'node_comment_count' => 'node:comment-count',
105     'unread_comment_count' => 'node:comment-count-new',
106     'log' => 'node:log',
107     //'' => '', // Date tokens expanded below
108     //'mod-' => '', // Date tokens expanded below
109     'menupath' => 'node:menu-link:parent:path][node:menu-link',
110     'menu' => 'node:menu-link:menu-name',
111     'menu-link-title' => 'node:menu-link',
112     'menu-link-mlid' => 'node:menu-link:mlid',
113     'menu-link-plid' => 'node:menu-link:parent:mlid',
114     //'term' => 'node:term',
115     //'term-id' => 'node:term:tid',
116     //'vocab' => 'node:term:vocabulary',
117     //'vocab-id' => 'node:term:vocabulary:vid',
118
119     // Book tokens
120     //'book' => 'node:book',
121     //'book_id' => 'node:book:bid',
122     //'bookpath' => 'node:book:path',
123
124     // Taxonomy tokens
125     'tid' => 'term:tid',
126     'cat' => 'term:name',
127     'cat-description' => 'term:description',
128     'vid' => 'term:vocabulary:vid',
129     'vocab' => 'term:vocabulary',
130     'vocab-description' => 'term:vocabulary:description',
131
132     // User tokens
133     'user' => 'user:name',
134     'uid' => 'user:uid',
135     'mail' => 'user:mail',
136     'reg-date' => 'user:created',
137     'reg-since' => 'user:created:since',
138     //'user-created' => '', // Date tokens expanded below
139     'log-date' => 'user:last-login',
140     'log-since' => 'user:last-login:since',
141     //'user-last-login' => '', // Date tokens expanded below
142     //'date-in-tz' => '',
143     'account-url' => 'user:url',
144     'account-edit' => 'user:edit-url',
145   ];
146
147   // Account for date tokens which need to be expanded.
148   $tokens += _token_upgrade_token_date_list('site-', 'site:date');
149   $tokens += _token_upgrade_token_date_list('', 'node:created');
150   $tokens += _token_upgrade_token_date_list('mod-', 'node:changed');
151   //$tokens += _token_upgrade_token_date_list('node-revision-', 'node:changed');
152   $tokens += _token_upgrade_token_date_list('comment-', 'comment:created');
153   $tokens += _token_upgrade_token_date_list('user-register-', 'user:created');
154   $tokens += _token_upgrade_token_date_list('user-last-login-', 'user:last-login');
155
156   return $tokens;
157 }
158
159 /**
160  * Build a list of Drupal 6 date tokens and their Drupal 7 token names.
161  */
162 function _token_upgrade_token_date_list($old_token, $new_token) {
163   $tokens = [];
164   $formats = [
165     'yyyy' => 'Y',
166     'yy' => 'y',
167     'month' => 'F',
168     'mon' => 'M',
169     'mm' => 'm',
170     'm' => 'n',
171     'ww' => 'W',
172     'date' => 'N',
173     'day' => 'l',
174     'ddd' => 'D',
175     'dd' => 'd',
176     'd' => 'j',
177   ];
178   foreach ($formats as $token_format => $date_format) {
179     $tokens[$old_token . $token_format] = "$new_token:custom:$date_format";
180   }
181   $tokens[$old_token . 'raw'] = "$new_token:raw";
182   $tokens[$old_token . 'since'] = "$new_token:since";
183   return $tokens;
184 }
185
186 /**
187  * Update a string containing Drupal 6 style tokens to Drupal 7 style tokens.
188  *
189  * @param $text
190  *   A string containing tokens.
191  * @param $updates
192  *   An optional array of Drupal 7 tokens keyed by their Drupal 6 token name.
193  *   The default tokens will be merged into this array. Note neither the old
194  *   or new token names should include the surrounding bracket ([ and ])
195  *   characters.
196  * @return
197  *   A string with the tokens upgraded
198  *
199  * @see _token_upgrade_token_list()
200  */
201 function token_update_token_text($text, $updates = [], $leading = '[', $trailing = ']') {
202   $updates += _token_upgrade_token_list();
203   $regex = '/' . preg_quote($leading, '/') . '([^\s]*)' . preg_quote($trailing, '/') . '/';
204   preg_match_all($regex, $text, $matches);
205
206   foreach ($matches[1] as $index => $old_token) {
207     if (isset($updates[$old_token])) {
208       $new_token = $updates[$old_token];
209       $text = str_replace("{$leading}{$old_token}{$trailing}", "[$new_token]", $text);
210       // Also replace any tokens that have a -raw suffix.
211       $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[$new_token]", $text);
212     }
213   }
214
215   return $text;
216 }
217
218 /**
219  * Get token problems.
220  */
221 function token_get_token_problems() {
222   // @todo Improve the duplicate checking to report which modules are the offenders.
223   //$token_info = [];
224   //foreach (module_implements('token_info') as $module) {
225   //  $module_token_info = module_invoke($module, 'token_info');
226   //  if (in_array($module, _token_core_supported_modules())) {
227   //    $module .= '/token';
228   //  }
229   //  if (isset($module_token_info['types'])) {
230   //    if (is_array($module_token_info['types'])) {
231   //      foreach (array_keys($module_token_info['types']) as $type) {
232   //        if (is_array($module_token_info['types'][$type])) {
233   //          $module_token_info['types'][$type] += ['module' => $module];
234   //        }
235   //      }
236   //    }
237   //  }
238   //  if (isset($module_token_info['tokens'])) {
239   //    if (is_array($module_token_info['tokens'])) {
240   //
241   //    }
242   //  }
243   //  if (is_array($module_token_info)) {
244   //    $token_info = array_merge_recursive($token_info, $module_token_info);
245   //  }
246   //}
247
248   $token_info = \Drupal::token()->getInfo();
249   $token_problems = [
250     'not-array' => [
251       'label' => t('Tokens or token types not defined as arrays'),
252       'severity' => REQUIREMENT_ERROR,
253     ],
254     'missing-info' => [
255       'label' => t('Tokens or token types missing name property'),
256       'severity' => REQUIREMENT_WARNING,
257     ],
258     'type-no-tokens' => [
259       'label' => t('Token types do not have any tokens defined'),
260       'severity' => REQUIREMENT_INFO,
261     ],
262     'tokens-no-type' => [
263       'label' => t('Token types are not defined but have tokens'),
264       'severity' => REQUIREMENT_INFO,
265     ],
266     'duplicate' => [
267       'label' => t('Token or token types are defined by multiple modules'),
268       'severity' => REQUIREMENT_ERROR,
269     ],
270   ];
271
272   // Check token types for problems.
273   foreach ($token_info['types'] as $type => $type_info) {
274     $real_type = !empty($type_info['type']) ? $type_info['type'] : $type;
275     if (!is_array($type_info)) {
276       $token_problems['not-array']['problems'][] = "\$info['types']['$type']";
277       continue;
278     }
279     elseif (!isset($type_info['name'])) {
280       $token_problems['missing-info']['problems'][] = "\$info['types']['$type']";
281     }
282     elseif (is_array($type_info['name'])) {
283       $token_problems['duplicate']['problems'][] = "\$info['types']['$type']";
284     }
285     elseif (empty($token_info['tokens'][$real_type])) {
286       $token_problems['type-no-tokens']['problems'][] = "\$info['types']['$real_type']";
287     }
288   }
289
290   // Check tokens for problems.
291   foreach ($token_info['tokens'] as $type => $tokens) {
292     if (!is_array($tokens)) {
293       $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']";
294       continue;
295     }
296     else {
297       foreach (array_keys($tokens) as $token) {
298         if (!is_array($tokens[$token])) {
299           $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']['$token']";
300           continue;
301         }
302         elseif (!isset($tokens[$token]['name'])) {
303           $token_problems['missing-info']['problems'][] = "\$info['tokens']['$type']['$token']";
304         }
305         elseif (is_array($tokens[$token]['name'])) {
306           $token_problems['duplicate']['problems'][] = "\$info['tokens']['$type']['$token']";
307         }
308       }
309     }
310     if (!isset($token_info['types'][$type])) {
311       $token_problems['tokens-no-type']['problems'][] = "\$info['types']['$type']";
312     }
313   }
314
315   return $token_problems;
316 }