user.tokens.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. }