user.tokens.inc

You are here

  1. 7 modules/user/user.tokens.inc
  2. 8 core/modules/user/user.tokens.inc

Builds placeholder replacement tokens for user-related data.

Functions

Namesort descending Description
user_tokens Implements hook_tokens().
user_token_info Implements hook_token_info().

File

modules/user/user.tokens.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for user-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function user_token_info() {
  10. $types['user'] = array(
  11. 'name' => t('Users'),
  12. 'description' => t('Tokens related to individual user accounts.'),
  13. 'needs-data' => 'user',
  14. );
  15. $types['current-user'] = array(
  16. 'name' => t('Current user'),
  17. 'description' => t('Tokens related to the currently logged in user.'),
  18. 'type' => 'user',
  19. );
  20. $user['uid'] = array(
  21. 'name' => t('User ID'),
  22. 'description' => t("The unique ID of the user account."),
  23. );
  24. $user['name'] = array(
  25. 'name' => t("Name"),
  26. 'description' => t("The login name of the user account."),
  27. );
  28. $user['mail'] = array(
  29. 'name' => t("Email"),
  30. 'description' => t("The email address of the user account."),
  31. );
  32. $user['url'] = array(
  33. 'name' => t("URL"),
  34. 'description' => t("The URL of the account profile page."),
  35. );
  36. $user['edit-url'] = array(
  37. 'name' => t("Edit URL"),
  38. 'description' => t("The URL of the account edit page."),
  39. );
  40. $user['last-login'] = array(
  41. 'name' => t("Last login"),
  42. 'description' => t("The date the user last logged in to the site."),
  43. 'type' => 'date',
  44. );
  45. $user['created'] = array(
  46. 'name' => t("Created"),
  47. 'description' => t("The date the user account was created."),
  48. 'type' => 'date',
  49. );
  50. return array(
  51. 'types' => $types,
  52. 'tokens' => array('user' => $user),
  53. );
  54. }
  55. /**
  56. * Implements hook_tokens().
  57. */
  58. function user_tokens($type, $tokens, array $data = array(), array $options = array()) {
  59. $url_options = array('absolute' => TRUE);
  60. if (isset($options['language'])) {
  61. $url_options['language'] = $options['language'];
  62. $language_code = $options['language']->language;
  63. }
  64. else {
  65. $language_code = NULL;
  66. }
  67. $sanitize = !empty($options['sanitize']);
  68. $replacements = array();
  69. if ($type == 'user' && !empty($data['user'])) {
  70. $account = $data['user'];
  71. foreach ($tokens as $name => $original) {
  72. switch ($name) {
  73. // Basic user account information.
  74. case 'uid':
  75. // In the case of hook user_presave uid is not set yet.
  76. $replacements[$original] = !empty($account->uid) ? $account->uid : t('not yet assigned');
  77. break;
  78. case 'name':
  79. $name = format_username($account);
  80. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  81. break;
  82. case 'mail':
  83. $replacements[$original] = $sanitize ? check_plain($account->mail) : $account->mail;
  84. break;
  85. case 'url':
  86. $replacements[$original] = !empty($account->uid) ? url("user/$account->uid", $url_options) : t('not yet assigned');
  87. break;
  88. case 'edit-url':
  89. $replacements[$original] = !empty($account->uid) ? url("user/$account->uid/edit", $url_options) : t('not yet assigned');
  90. break;
  91. // These tokens are default variations on the chained tokens handled below.
  92. case 'last-login':
  93. $replacements[$original] = !empty($account->login) ? format_date($account->login, 'medium', '', NULL, $language_code) : t('never');
  94. break;
  95. case 'created':
  96. // In the case of user_presave the created date may not yet be set.
  97. $replacements[$original] = !empty($account->created) ? format_date($account->created, 'medium', '', NULL, $language_code) : t('not yet created');
  98. break;
  99. }
  100. }
  101. if ($login_tokens = token_find_with_prefix($tokens, 'last-login')) {
  102. $replacements += token_generate('date', $login_tokens, array('date' => $account->login), $options);
  103. }
  104. if ($registered_tokens = token_find_with_prefix($tokens, 'created')) {
  105. $replacements += token_generate('date', $registered_tokens, array('date' => $account->created), $options);
  106. }
  107. }
  108. if ($type == 'current-user') {
  109. $account = user_load($GLOBALS['user']->uid);
  110. $replacements += token_generate('user', $tokens, array('user' => $account), $options);
  111. }
  112. return $replacements;
  113. }