5 * Install, update and uninstall functions for the token module.
9 * Implements hook_requirements().
11 function token_requirements($phase = 'runtime') {
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']);
23 '#theme' => 'item_list',
24 '#items' => $problems,
27 $requirements['token-' . $problem_key] = [
28 'title' => $problem['label'],
29 'value' => \Drupal::service('renderer')->renderPlain($build),
30 'severity' => $problem['severity'],
40 * Implements hook_install().
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()) {
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")) {
55 'targetEntityType' => $entity_type,
56 'id' => "$entity_type.token",
58 'label' => t('Token'),
65 * Build a list of Drupal 6 tokens and their Drupal 7 token names.
67 function _token_upgrade_token_list() {
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',
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',
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',
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',
120 //'book' => 'node:book',
121 //'book_id' => 'node:book:bid',
122 //'bookpath' => 'node:book:path',
126 'cat' => 'term:name',
127 'cat-description' => 'term:description',
128 'vid' => 'term:vocabulary:vid',
129 'vocab' => 'term:vocabulary',
130 'vocab-description' => 'term:vocabulary:description',
133 'user' => 'user:name',
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',
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');
160 * Build a list of Drupal 6 date tokens and their Drupal 7 token names.
162 function _token_upgrade_token_date_list($old_token, $new_token) {
178 foreach ($formats as $token_format => $date_format) {
179 $tokens[$old_token . $token_format] = "$new_token:custom:$date_format";
181 $tokens[$old_token . 'raw'] = "$new_token:raw";
182 $tokens[$old_token . 'since'] = "$new_token:since";
187 * Update a string containing Drupal 6 style tokens to Drupal 7 style tokens.
190 * A string containing tokens.
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 ])
197 * A string with the tokens upgraded
199 * @see _token_upgrade_token_list()
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);
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);
219 * Get token problems.
221 function token_get_token_problems() {
222 // @todo Improve the duplicate checking to report which modules are the offenders.
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';
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];
238 // if (isset($module_token_info['tokens'])) {
239 // if (is_array($module_token_info['tokens'])) {
243 // if (is_array($module_token_info)) {
244 // $token_info = array_merge_recursive($token_info, $module_token_info);
248 $token_info = \Drupal::token()->getInfo();
251 'label' => t('Tokens or token types not defined as arrays'),
252 'severity' => REQUIREMENT_ERROR,
255 'label' => t('Tokens or token types missing name property'),
256 'severity' => REQUIREMENT_WARNING,
258 'type-no-tokens' => [
259 'label' => t('Token types do not have any tokens defined'),
260 'severity' => REQUIREMENT_INFO,
262 'tokens-no-type' => [
263 'label' => t('Token types are not defined but have tokens'),
264 'severity' => REQUIREMENT_INFO,
267 'label' => t('Token or token types are defined by multiple modules'),
268 'severity' => REQUIREMENT_ERROR,
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']";
279 elseif (!isset($type_info['name'])) {
280 $token_problems['missing-info']['problems'][] = "\$info['types']['$type']";
282 elseif (is_array($type_info['name'])) {
283 $token_problems['duplicate']['problems'][] = "\$info['types']['$type']";
285 elseif (empty($token_info['tokens'][$real_type])) {
286 $token_problems['type-no-tokens']['problems'][] = "\$info['types']['$real_type']";
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']";
297 foreach (array_keys($tokens) as $token) {
298 if (!is_array($tokens[$token])) {
299 $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']['$token']";
302 elseif (!isset($tokens[$token]['name'])) {
303 $token_problems['missing-info']['problems'][] = "\$info['tokens']['$type']['$token']";
305 elseif (is_array($tokens[$token]['name'])) {
306 $token_problems['duplicate']['problems'][] = "\$info['tokens']['$type']['$token']";
310 if (!isset($token_info['types'][$type])) {
311 $token_problems['tokens-no-type']['problems'][] = "\$info['types']['$type']";
315 return $token_problems;