user.tokens.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $user['changed'] = array(
  51. 'name' => t("Changed"),
  52. 'description' => t("The date the user account was changed."),
  53. 'type' => 'date',
  54. );
  55. return array(
  56. 'types' => $types,
  57. 'tokens' => array('user' => $user),
  58. );
  59. }
  60. /**
  61. * Implements hook_tokens().
  62. */
  63. function user_tokens($type, $tokens, array $data = array(), array $options = array()) {
  64. $url_options = array('absolute' => TRUE);
  65. if (isset($options['language'])) {
  66. $url_options['language'] = $options['language'];
  67. $language_code = $options['language']->language;
  68. }
  69. else {
  70. $language_code = NULL;
  71. }
  72. $sanitize = !empty($options['sanitize']);
  73. $replacements = array();
  74. if ($type == 'user' && !empty($data['user'])) {
  75. $account = $data['user'];
  76. foreach ($tokens as $name => $original) {
  77. switch ($name) {
  78. // Basic user account information.
  79. case 'uid':
  80. // In the case of hook user_presave uid is not set yet.
  81. $replacements[$original] = !empty($account->uid) ? $account->uid : t('not yet assigned');
  82. break;
  83. case 'name':
  84. $name = format_username($account);
  85. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  86. break;
  87. case 'mail':
  88. if (empty($account->mail)) {
  89. $replacements[$original] = '';
  90. }
  91. else {
  92. $replacements[$original] = $sanitize ? check_plain($account->mail) : $account->mail;
  93. }
  94. break;
  95. case 'url':
  96. $replacements[$original] = !empty($account->uid) ? url("user/$account->uid", $url_options) : t('not yet assigned');
  97. break;
  98. case 'edit-url':
  99. $replacements[$original] = !empty($account->uid) ? url("user/$account->uid/edit", $url_options) : t('not yet assigned');
  100. break;
  101. // These tokens are default variations on the chained tokens handled below.
  102. case 'last-login':
  103. $replacements[$original] = !empty($account->login) ? format_date($account->login, 'medium', '', NULL, $language_code) : t('never');
  104. break;
  105. case 'created':
  106. // In the case of user_presave the created date may not yet be set.
  107. $replacements[$original] = !empty($account->created) ? format_date($account->created, 'medium', '', NULL, $language_code) : t('not yet created');
  108. break;
  109. case 'changed':
  110. // In the case of user_presave the created date may not yet be set.
  111. $replacements[$original] = !empty($account->changed) ? format_date($account->changed, 'medium', '', NULL, $language_code) : t('not yet created');
  112. break;
  113. }
  114. }
  115. if ($login_tokens = token_find_with_prefix($tokens, 'last-login')) {
  116. $replacements += token_generate('date', $login_tokens, array('date' => $account->login), $options);
  117. }
  118. if ($registered_tokens = token_find_with_prefix($tokens, 'created')) {
  119. $replacements += token_generate('date', $registered_tokens, array('date' => $account->created), $options);
  120. }
  121. if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
  122. $replacements += token_generate('date', $changed_tokens, array('date' => $account->changed), $options);
  123. }
  124. }
  125. if ($type == 'current-user') {
  126. $account = user_load($GLOBALS['user']->uid);
  127. $replacements += token_generate('user', $tokens, array('user' => $account), $options);
  128. }
  129. return $replacements;
  130. }