-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.install
162 lines (150 loc) · 5.04 KB
/
token.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* @file
* Install, update and uninstall functions for the token module.
*/
/**
* Implements hook_requirements().
*/
function token_requirements($phase = 'runtime') {
$requirements = [];
if ($phase == 'runtime') {
// Check for various token definition problems.
$token_problems = token_get_token_problems();
// Format and display each token problem.
foreach ($token_problems as $problem_key => $problem) {
if (!empty($problem['problems'])) {
$problems = array_unique($problem['problems']);
$build = [
'#theme' => 'item_list',
'#items' => $problems,
];
$requirements['token-' . $problem_key] = [
'title' => $problem['label'],
'value' => \Drupal::service('renderer')->renderPlain($build),
'severity' => $problem['severity'],
];
}
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function token_install() {
// Create a token view mode for each entity type.
$info = \Drupal::entityTypeManager()->getDefinitions();
foreach ($info as $entity_type => $entity_type_info) {
// We're only interested in entity types with a view builder.
if (!$entity_type_info->getViewBuilderClass()) {
continue;
}
// Try to find a token view mode for that entity type.
$storage = \Drupal::entityTypeManager()->getStorage('entity_view_mode');
// Add a token view mode if it does not already exist.
if (!$storage->load("$entity_type.token")) {
$storage->create([
'targetEntityType' => $entity_type,
'id' => "$entity_type.token",
'status' => TRUE,
'label' => t('Token'),
])->save();
}
}
}
/**
* Get token problems.
*/
function token_get_token_problems() {
// @todo Improve the duplicate checking to report which modules are the offenders.
//$token_info = [];
//foreach (module_implements('token_info') as $module) {
// $module_token_info = module_invoke($module, 'token_info');
// if (in_array($module, _token_core_supported_modules())) {
// $module .= '/token';
// }
// if (isset($module_token_info['types'])) {
// if (is_array($module_token_info['types'])) {
// foreach (array_keys($module_token_info['types']) as $type) {
// if (is_array($module_token_info['types'][$type])) {
// $module_token_info['types'][$type] += ['module' => $module];
// }
// }
// }
// }
// if (isset($module_token_info['tokens'])) {
// if (is_array($module_token_info['tokens'])) {
//
// }
// }
// if (is_array($module_token_info)) {
// $token_info = array_merge_recursive($token_info, $module_token_info);
// }
//}
$token_info = \Drupal::token()->getInfo();
$token_problems = [
'not-array' => [
'label' => t('Tokens or token types not defined as arrays'),
'severity' => REQUIREMENT_ERROR,
],
'missing-info' => [
'label' => t('Tokens or token types missing name property'),
'severity' => REQUIREMENT_WARNING,
],
'type-no-tokens' => [
'label' => t('Token types do not have any tokens defined'),
'severity' => REQUIREMENT_INFO,
],
'tokens-no-type' => [
'label' => t('Token types are not defined but have tokens'),
'severity' => REQUIREMENT_INFO,
],
'duplicate' => [
'label' => t('Token or token types are defined by multiple modules'),
'severity' => REQUIREMENT_ERROR,
],
];
// Check token types for problems.
foreach ($token_info['types'] as $type => $type_info) {
$real_type = !empty($type_info['type']) ? $type_info['type'] : $type;
if (!is_array($type_info)) {
$token_problems['not-array']['problems'][] = "\$info['types']['$type']";
continue;
}
elseif (!isset($type_info['name'])) {
$token_problems['missing-info']['problems'][] = "\$info['types']['$type']";
}
elseif (is_array($type_info['name'])) {
$token_problems['duplicate']['problems'][] = "\$info['types']['$type']";
}
elseif (empty($token_info['tokens'][$real_type])) {
$token_problems['type-no-tokens']['problems'][] = "\$info['types']['$real_type']";
}
}
// Check tokens for problems.
foreach ($token_info['tokens'] as $type => $tokens) {
if (!is_array($tokens)) {
$token_problems['not-array']['problems'][] = "\$info['tokens']['$type']";
continue;
}
else {
foreach (array_keys($tokens) as $token) {
if (!is_array($tokens[$token])) {
$token_problems['not-array']['problems'][] = "\$info['tokens']['$type']['$token']";
continue;
}
elseif (!isset($tokens[$token]['name'])) {
$token_problems['missing-info']['problems'][] = "\$info['tokens']['$type']['$token']";
}
elseif (is_array($tokens[$token]['name'])) {
$token_problems['duplicate']['problems'][] = "\$info['tokens']['$type']['$token']";
}
}
}
if (!isset($token_info['types'][$type])) {
$token_problems['tokens-no-type']['problems'][] = "\$info['types']['$type']";
}
}
return $token_problems;
}