5 * Builds placeholder replacement tokens for user-related data.
8 use Drupal\Core\Datetime\Entity\DateFormat;
9 use Drupal\Core\Render\BubbleableMetadata;
10 use Drupal\user\Entity\User;
13 * Implements hook_token_info().
15 function user_token_info() {
18 'description' => t('Tokens related to individual user accounts.'),
19 'needs-data' => 'user',
21 $types['current-user'] = [
22 'name' => t('Current user'),
23 'description' => t('Tokens related to the currently logged in user.'),
28 'name' => t('User ID'),
29 'description' => t("The unique ID of the user account."),
32 'name' => t("Deprecated: User Name"),
33 'description' => t("Deprecated: Use account-name or display-name instead."),
35 $user['account-name'] = [
36 'name' => t("Account Name"),
37 'description' => t("The login name of the user account."),
39 $user['display-name'] = [
40 'name' => t("Display Name"),
41 'description' => t("The display name of the user account."),
45 'description' => t("The email address of the user account."),
49 'description' => t("The URL of the account profile page."),
52 'name' => t("Edit URL"),
53 'description' => t("The URL of the account edit page."),
56 $user['last-login'] = [
57 'name' => t("Last login"),
58 'description' => t("The date the user last logged in to the site."),
62 'name' => t("Created"),
63 'description' => t("The date the user account was created."),
69 'tokens' => ['user' => $user],
74 * Implements hook_tokens().
76 function user_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
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'];
89 if ($type == 'user' && !empty($data['user'])) {
90 /** @var \Drupal\user\UserInterface $account */
91 $account = $data['user'];
92 foreach ($tokens as $name => $original) {
94 // Basic user account information.
96 // In the case of hook user_presave uid is not set yet.
97 $replacements[$original] = $account->id() ?: t('not yet assigned');
101 $replacements[$original] = $account->getDisplayName();
102 if ($account->isAnonymous()) {
103 $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
109 $display_name = $account->getAccountName();
110 $replacements[$original] = $display_name;
111 if ($account->isAnonymous()) {
112 $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
117 $replacements[$original] = $account->getEmail();
121 $replacements[$original] = $account->id() ? $account->url('canonical', $url_options) : t('not yet assigned');
125 $replacements[$original] = $account->id() ? $account->url('edit-form', $url_options) : t('not yet assigned');
128 // These tokens are default variations on the chained tokens handled below.
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');
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');
144 if ($login_tokens = $token_service->findWithPrefix($tokens, 'last-login')) {
145 $replacements += $token_service->generate('date', $login_tokens, ['date' => $account->getLastLoginTime()], $options, $bubbleable_metadata);
148 if ($registered_tokens = $token_service->findWithPrefix($tokens, 'created')) {
149 $replacements += $token_service->generate('date', $registered_tokens, ['date' => $account->getCreatedTime()], $options, $bubbleable_metadata);
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);
159 return $replacements;