Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Builds placeholder replacement tokens for user-related data.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Datetime\Entity\DateFormat;
|
Chris@0
|
9 use Drupal\Core\Render\BubbleableMetadata;
|
Chris@0
|
10 use Drupal\user\Entity\User;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Implements hook_token_info().
|
Chris@0
|
14 */
|
Chris@0
|
15 function user_token_info() {
|
Chris@0
|
16 $types['user'] = [
|
Chris@0
|
17 'name' => t('Users'),
|
Chris@0
|
18 'description' => t('Tokens related to individual user accounts.'),
|
Chris@0
|
19 'needs-data' => 'user',
|
Chris@0
|
20 ];
|
Chris@0
|
21 $types['current-user'] = [
|
Chris@0
|
22 'name' => t('Current user'),
|
Chris@0
|
23 'description' => t('Tokens related to the currently logged in user.'),
|
Chris@0
|
24 'type' => 'user',
|
Chris@0
|
25 ];
|
Chris@0
|
26
|
Chris@0
|
27 $user['uid'] = [
|
Chris@0
|
28 'name' => t('User ID'),
|
Chris@0
|
29 'description' => t("The unique ID of the user account."),
|
Chris@0
|
30 ];
|
Chris@0
|
31 $user['name'] = [
|
Chris@0
|
32 'name' => t("Deprecated: User Name"),
|
Chris@0
|
33 'description' => t("Deprecated: Use account-name or display-name instead."),
|
Chris@0
|
34 ];
|
Chris@0
|
35 $user['account-name'] = [
|
Chris@0
|
36 'name' => t("Account Name"),
|
Chris@0
|
37 'description' => t("The login name of the user account."),
|
Chris@0
|
38 ];
|
Chris@0
|
39 $user['display-name'] = [
|
Chris@0
|
40 'name' => t("Display Name"),
|
Chris@0
|
41 'description' => t("The display name of the user account."),
|
Chris@0
|
42 ];
|
Chris@0
|
43 $user['mail'] = [
|
Chris@0
|
44 'name' => t("Email"),
|
Chris@0
|
45 'description' => t("The email address of the user account."),
|
Chris@0
|
46 ];
|
Chris@0
|
47 $user['url'] = [
|
Chris@0
|
48 'name' => t("URL"),
|
Chris@0
|
49 'description' => t("The URL of the account profile page."),
|
Chris@0
|
50 ];
|
Chris@0
|
51 $user['edit-url'] = [
|
Chris@0
|
52 'name' => t("Edit URL"),
|
Chris@0
|
53 'description' => t("The URL of the account edit page."),
|
Chris@0
|
54 ];
|
Chris@0
|
55
|
Chris@0
|
56 $user['last-login'] = [
|
Chris@0
|
57 'name' => t("Last login"),
|
Chris@0
|
58 'description' => t("The date the user last logged in to the site."),
|
Chris@0
|
59 'type' => 'date',
|
Chris@0
|
60 ];
|
Chris@0
|
61 $user['created'] = [
|
Chris@0
|
62 'name' => t("Created"),
|
Chris@0
|
63 'description' => t("The date the user account was created."),
|
Chris@0
|
64 'type' => 'date',
|
Chris@0
|
65 ];
|
Chris@0
|
66
|
Chris@0
|
67 return [
|
Chris@0
|
68 'types' => $types,
|
Chris@0
|
69 'tokens' => ['user' => $user],
|
Chris@0
|
70 ];
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * Implements hook_tokens().
|
Chris@0
|
75 */
|
Chris@0
|
76 function user_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
|
Chris@0
|
77
|
Chris@0
|
78 $token_service = \Drupal::token();
|
Chris@0
|
79 $url_options = ['absolute' => TRUE];
|
Chris@0
|
80 if (isset($options['langcode'])) {
|
Chris@0
|
81 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
|
Chris@0
|
82 $langcode = $options['langcode'];
|
Chris@0
|
83 }
|
Chris@0
|
84 else {
|
Chris@0
|
85 $langcode = NULL;
|
Chris@0
|
86 }
|
Chris@0
|
87 $replacements = [];
|
Chris@0
|
88
|
Chris@0
|
89 if ($type == 'user' && !empty($data['user'])) {
|
Chris@0
|
90 /** @var \Drupal\user\UserInterface $account */
|
Chris@0
|
91 $account = $data['user'];
|
Chris@0
|
92 foreach ($tokens as $name => $original) {
|
Chris@0
|
93 switch ($name) {
|
Chris@0
|
94 // Basic user account information.
|
Chris@0
|
95 case 'uid':
|
Chris@0
|
96 // In the case of hook user_presave uid is not set yet.
|
Chris@0
|
97 $replacements[$original] = $account->id() ?: t('not yet assigned');
|
Chris@0
|
98 break;
|
Chris@0
|
99
|
Chris@0
|
100 case 'display-name':
|
Chris@0
|
101 $replacements[$original] = $account->getDisplayName();
|
Chris@0
|
102 if ($account->isAnonymous()) {
|
Chris@0
|
103 $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
|
Chris@0
|
104 }
|
Chris@0
|
105 break;
|
Chris@0
|
106
|
Chris@0
|
107 case 'name':
|
Chris@0
|
108 case 'account-name':
|
Chris@0
|
109 $display_name = $account->getAccountName();
|
Chris@0
|
110 $replacements[$original] = $display_name;
|
Chris@0
|
111 if ($account->isAnonymous()) {
|
Chris@0
|
112 $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
|
Chris@0
|
113 }
|
Chris@0
|
114 break;
|
Chris@0
|
115
|
Chris@0
|
116 case 'mail':
|
Chris@0
|
117 $replacements[$original] = $account->getEmail();
|
Chris@0
|
118 break;
|
Chris@0
|
119
|
Chris@0
|
120 case 'url':
|
Chris@18
|
121 $replacements[$original] = $account->id() ? $account->toUrl('canonical', $url_options)->toString() : t('not yet assigned');
|
Chris@0
|
122 break;
|
Chris@0
|
123
|
Chris@0
|
124 case 'edit-url':
|
Chris@18
|
125 $replacements[$original] = $account->id() ? $account->toUrl('edit-form', $url_options)->toString() : t('not yet assigned');
|
Chris@0
|
126 break;
|
Chris@0
|
127
|
Chris@0
|
128 // These tokens are default variations on the chained tokens handled below.
|
Chris@0
|
129 case 'last-login':
|
Chris@0
|
130 $date_format = DateFormat::load('medium');
|
Chris@0
|
131 $bubbleable_metadata->addCacheableDependency($date_format);
|
Chris@18
|
132 $replacements[$original] = $account->getLastLoginTime() ? \Drupal::service('date.formatter')->format($account->getLastLoginTime(), 'medium', '', NULL, $langcode) : t('never');
|
Chris@0
|
133 break;
|
Chris@0
|
134
|
Chris@0
|
135 case 'created':
|
Chris@0
|
136 $date_format = DateFormat::load('medium');
|
Chris@0
|
137 $bubbleable_metadata->addCacheableDependency($date_format);
|
Chris@0
|
138 // In the case of user_presave the created date may not yet be set.
|
Chris@18
|
139 $replacements[$original] = $account->getCreatedTime() ? \Drupal::service('date.formatter')->format($account->getCreatedTime(), 'medium', '', NULL, $langcode) : t('not yet created');
|
Chris@0
|
140 break;
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 if ($login_tokens = $token_service->findWithPrefix($tokens, 'last-login')) {
|
Chris@0
|
145 $replacements += $token_service->generate('date', $login_tokens, ['date' => $account->getLastLoginTime()], $options, $bubbleable_metadata);
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 if ($registered_tokens = $token_service->findWithPrefix($tokens, 'created')) {
|
Chris@0
|
149 $replacements += $token_service->generate('date', $registered_tokens, ['date' => $account->getCreatedTime()], $options, $bubbleable_metadata);
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 if ($type == 'current-user') {
|
Chris@0
|
154 $account = User::load(\Drupal::currentUser()->id());
|
Chris@0
|
155 $bubbleable_metadata->addCacheContexts(['user']);
|
Chris@0
|
156 $replacements += $token_service->generate('user', $tokens, ['user' => $account], $options, $bubbleable_metadata);
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 return $replacements;
|
Chris@0
|
160 }
|