user.tokens.inc 5.19 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Builds placeholder replacement tokens for user-related data.
 */

8 9
use Drupal\Core\Datetime\Entity\DateFormat;
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 18 19
    'name' => t('Users'),
    'description' => t('Tokens related to individual user accounts.'),
    'needs-data' => 'user',
20 21
  ];
  $types['current-user'] = [
22 23 24
    'name' => t('Current user'),
    'description' => t('Tokens related to the currently logged in user.'),
    'type' => 'user',
25
  ];
26

27
  $user['uid'] = [
28 29
    'name' => t('User ID'),
    'description' => t("The unique ID of the user account."),
30 31
  ];
  $user['name'] = [
32 33
    'name' => t("Deprecated: User Name"),
    '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 41
    'name' => t("Display Name"),
    'description' => t("The display name of the user account."),
42 43
  ];
  $user['mail'] = [
44 45
    'name' => t("Email"),
    'description' => t("The email address of the user account."),
46 47
  ];
  $user['url'] = [
48 49
    'name' => t("URL"),
    '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 58 59
    'name' => t("Last login"),
    'description' => t("The date the user last logged in to the site."),
    'type' => 'date',
60 61
  ];
  $user['created'] = [
62 63 64
    'name' => t("Created"),
    'description' => t("The date the user account was created."),
    'type' => 'date',
65
  ];
66

67
  return [
68
    'types' => $types,
69 70
    'tokens' => ['user' => $user],
  ];
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 92 93 94 95
    $account = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        // Basic user account information.
        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 99
          break;

100 101 102 103 104 105 106
        case 'display-name':
          $replacements[$original] = $account->getDisplayName();
          if ($account->isAnonymous()) {
            $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
          }
          break;

107
        case 'name':
108 109
        case 'account-name':
          $display_name = $account->getAccountName();
110
          $replacements[$original] = $display_name;
111 112 113
          if ($account->isAnonymous()) {
            $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
          }
114 115 116
          break;

        case 'mail':
117
          $replacements[$original] = $account->getEmail();
118 119 120
          break;

        case 'url':
121
          $replacements[$original] = $account->id() ? $account->url('canonical', $url_options) : t('not yet assigned');
122 123 124
          break;

        case 'edit-url':
125
          $replacements[$original] = $account->id() ? $account->url('edit-form', $url_options) : t('not yet assigned');
126 127 128 129
          break;

        // These tokens are default variations on the chained tokens handled below.
        case 'last-login':
130 131
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
132
          $replacements[$original] = $account->getLastLoginTime() ? format_date($account->getLastLoginTime(), 'medium', '', NULL, $langcode) : t('never');
133 134 135
          break;

        case 'created':
136 137
          $date_format = DateFormat::load('medium');
          $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 141 142 143
          break;
      }
    }

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 160
  }

  return $replacements;
}