-
Notifications
You must be signed in to change notification settings - Fork 0
/
uitid.tokens.inc
84 lines (70 loc) · 1.99 KB
/
uitid.tokens.inc
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
<?php
/**
* @file
* Tokens for UiTiD module.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function uitid_token_info() {
$info = [];
$info['types']['uitiduser'] = [
'name' => t('UiTID user'),
'description' => t('Custom tokens with values from the active UiTID user.'),
];
$info['tokens']['uitiduser']['id'] = [
'name' => t("UiTID user's id"),
'description' => t('The ID of the active UiTID user.'),
];
$info['tokens']['uitiduser']['first_name'] = [
'name' => t("UiTID user's first name"),
'description' => t('The first name of the active UiTID user.'),
];
$info['tokens']['uitiduser']['email'] = [
'name' => t('UiTID user email'),
'description' => t('The email address of the active UiTID user.'),
];
return $info;
}
/**
* Implements hook_tokens().
*/
function uitid_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type === 'uitiduser') {
if (!isset($data['uitid_user'])) {
try {
/** @var \Drupal\uitid\UitIdCurrentUserInterface $currentUser */
$currentUser = \Drupal::service('uitid.current_user');
if ($currentUser->isUitIdUser()) {
$data['uitid_user'] = $currentUser->getUser();
}
}
catch (\Exception $e) {
// No error.
}
}
if (isset($data['uitid_user'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'first_name':
$replacements[$original] = $data['uitid_user']['https://publiq.be/first_name'] ?? '';
break;
case 'id':
$replacements[$original] = $data['uitid_user']['sub'] ?? '';
break;
case 'email':
$replacements[$original] = $data['uitid_user']['email'] ?? '';
break;
}
}
}
else {
foreach ($tokens as $original) {
$replacements[$original] = '';
}
}
}
return $replacements;
}