comparison core/modules/user/user.tokens.inc @ 0:c75dbcec494b

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