user.tokens.inc 5.2 KB

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