Chris@0: t('Users'), Chris@0: 'description' => t('Tokens related to individual user accounts.'), Chris@0: 'needs-data' => 'user', Chris@0: ]; Chris@0: $types['current-user'] = [ Chris@0: 'name' => t('Current user'), Chris@0: 'description' => t('Tokens related to the currently logged in user.'), Chris@0: 'type' => 'user', Chris@0: ]; Chris@0: Chris@0: $user['uid'] = [ Chris@0: 'name' => t('User ID'), Chris@0: 'description' => t("The unique ID of the user account."), Chris@0: ]; Chris@0: $user['name'] = [ Chris@0: 'name' => t("Deprecated: User Name"), Chris@0: 'description' => t("Deprecated: Use account-name or display-name instead."), Chris@0: ]; Chris@0: $user['account-name'] = [ Chris@0: 'name' => t("Account Name"), Chris@0: 'description' => t("The login name of the user account."), Chris@0: ]; Chris@0: $user['display-name'] = [ Chris@0: 'name' => t("Display Name"), Chris@0: 'description' => t("The display name of the user account."), Chris@0: ]; Chris@0: $user['mail'] = [ Chris@0: 'name' => t("Email"), Chris@0: 'description' => t("The email address of the user account."), Chris@0: ]; Chris@0: $user['url'] = [ Chris@0: 'name' => t("URL"), Chris@0: 'description' => t("The URL of the account profile page."), Chris@0: ]; Chris@0: $user['edit-url'] = [ Chris@0: 'name' => t("Edit URL"), Chris@0: 'description' => t("The URL of the account edit page."), Chris@0: ]; Chris@0: Chris@0: $user['last-login'] = [ Chris@0: 'name' => t("Last login"), Chris@0: 'description' => t("The date the user last logged in to the site."), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: $user['created'] = [ Chris@0: 'name' => t("Created"), Chris@0: 'description' => t("The date the user account was created."), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'types' => $types, Chris@0: 'tokens' => ['user' => $user], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_tokens(). Chris@0: */ Chris@0: function user_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { Chris@0: Chris@0: $token_service = \Drupal::token(); Chris@0: $url_options = ['absolute' => TRUE]; Chris@0: if (isset($options['langcode'])) { Chris@0: $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); Chris@0: $langcode = $options['langcode']; Chris@0: } Chris@0: else { Chris@0: $langcode = NULL; Chris@0: } Chris@0: $replacements = []; Chris@0: Chris@0: if ($type == 'user' && !empty($data['user'])) { Chris@0: /** @var \Drupal\user\UserInterface $account */ Chris@0: $account = $data['user']; Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: // Basic user account information. Chris@0: case 'uid': Chris@0: // In the case of hook user_presave uid is not set yet. Chris@0: $replacements[$original] = $account->id() ?: t('not yet assigned'); Chris@0: break; Chris@0: Chris@0: case 'display-name': Chris@0: $replacements[$original] = $account->getDisplayName(); Chris@0: if ($account->isAnonymous()) { Chris@0: $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings')); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'name': Chris@0: case 'account-name': Chris@0: $display_name = $account->getAccountName(); Chris@0: $replacements[$original] = $display_name; Chris@0: if ($account->isAnonymous()) { Chris@0: $bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings')); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'mail': Chris@0: $replacements[$original] = $account->getEmail(); Chris@0: break; Chris@0: Chris@0: case 'url': Chris@18: $replacements[$original] = $account->id() ? $account->toUrl('canonical', $url_options)->toString() : t('not yet assigned'); Chris@0: break; Chris@0: Chris@0: case 'edit-url': Chris@18: $replacements[$original] = $account->id() ? $account->toUrl('edit-form', $url_options)->toString() : t('not yet assigned'); Chris@0: break; Chris@0: Chris@0: // These tokens are default variations on the chained tokens handled below. Chris@0: case 'last-login': Chris@0: $date_format = DateFormat::load('medium'); Chris@0: $bubbleable_metadata->addCacheableDependency($date_format); Chris@18: $replacements[$original] = $account->getLastLoginTime() ? \Drupal::service('date.formatter')->format($account->getLastLoginTime(), 'medium', '', NULL, $langcode) : t('never'); Chris@0: break; Chris@0: Chris@0: case 'created': Chris@0: $date_format = DateFormat::load('medium'); Chris@0: $bubbleable_metadata->addCacheableDependency($date_format); Chris@0: // In the case of user_presave the created date may not yet be set. Chris@18: $replacements[$original] = $account->getCreatedTime() ? \Drupal::service('date.formatter')->format($account->getCreatedTime(), 'medium', '', NULL, $langcode) : t('not yet created'); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($login_tokens = $token_service->findWithPrefix($tokens, 'last-login')) { Chris@0: $replacements += $token_service->generate('date', $login_tokens, ['date' => $account->getLastLoginTime()], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if ($registered_tokens = $token_service->findWithPrefix($tokens, 'created')) { Chris@0: $replacements += $token_service->generate('date', $registered_tokens, ['date' => $account->getCreatedTime()], $options, $bubbleable_metadata); Chris@0: } Chris@0: } Chris@0: Chris@0: if ($type == 'current-user') { Chris@0: $account = User::load(\Drupal::currentUser()->id()); Chris@0: $bubbleable_metadata->addCacheContexts(['user']); Chris@0: $replacements += $token_service->generate('user', $tokens, ['user' => $account], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: return $replacements; Chris@0: }