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