Raw username token for Drupal 7

Public

Provides an unaltered username token. Username provided by this token is taken directly from the database and is unaltered via hook_username_alter(). More info when and why you may need this read in my blog: http://timonweb.com/raw-username-token-for-drupal-7

</> CopyGet raw version
php
  1. /**
  2.  * Provide information about our custom token.
  3.  *
  4.  * @see http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_token_info/7
  5.  * @return array
  6.  * An associative array of available tokens and token types..
  7.  */
  8. function MODULE_NAME_token_info() {
  9. $info['tokens']['user']['name_raw'] = array(
  10. 'name' => t('Name: raw'),
  11. 'description' => t('Raw username taken straight from DB.'),
  12. );
  13. return $info;
  14. }
  15.  
  16. /**
  17.  * Provide replacement values for placeholder tokens.
  18.  *
  19.  * @see http://api-drupal.org/api/drupal/modules--system--system.api.php/function/hook_tokens/7
  20.  * @param string $type
  21.  * The machine-readable name of the type (group) of token being replaced, such as
  22.  * 'node', 'user', or another type of defined by a hook_token_info() implementation.
  23.  * @param array $tokens
  24.  * An array of tokens to be replaced. The keys are the machine-readable token names,
  25.  * and the values are the raw [type:token] that appeared in the original text.
  26.  * @param array $data (optional)
  27.  * An associative array of data objects to be used when generating replacement values
  28.  * as supplied in the $data parameter to token_replace().
  29.  * @param array $options (optional)
  30.  * An associative array of options for token replacement; see token_replace for
  31.  * possible values.
  32.  *@return array
  33.  * An associative of replacement values, keyed by the raw [type:token] strings from
  34.  * from the original text.
  35.  */
  36. function MODULE_NAME_tokens($type, $tokens, array $data = array(), array $options = array()) {
  37. $replacements = array();
  38. if ($type == 'user' && !empty($data['user'])) {
  39. $user = $data['user'];
  40. foreach ($tokens as $name => $original) {
  41. switch ($name) {
  42. case 'name_raw':
  43. $replacements[$original] = $user->name;
  44. break;
  45. }
  46. }
  47. return $replacements;
  48. }
  49. }