Raw username token for Drupal 7

There are situations, when you or other modules alter username via hook_username_alter() and this can change username dramatically: for example, you can alter username into a combination of First and Last Name, etc.

Looks nice, but there's a problem, it will alter username into First and Last Name everywhere, even in admin emails being sent out to users upon registration and instead of real username your users will get an altered username in their mail which is sometimes not good. So what can be done? Hassling with hook_username_alter() and trying to catch cases when you can alter or not username isn't pleasant and here when tokens come to rescue. You can create a custom raw username token which holds unaltered username taken directly from database. Then you can put this token everywhere you need an unaltered username including admin emails.

So let's prepare our custom token, this is simple, put the code below into your custom module (don't forget to replace MODULE_NAME with your module name):

 * Provide information about our custom token.
function MODULE_NAME_token_info() {
  $info['tokens']['user']['name_raw'] = array(
    'name' => t('Name: raw'),
    'description' => t('Raw username taken straight from DB.'),
  return $info;
 * Provide replacement values for placeholder tokens.
function MODULE_NAME_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'user' && !empty($data['user'])) {
    $user = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name_raw':
          $replacements[$original] = $user->name;
    return $replacements;

And clear the cache. You should now have a User: Name Raw token available in the token list. Put it anywhere you need an unaltered username to be shown and profit!

You can find and clone this snippet here: http://dropbucket.org/node/905.


Submitted by Marc Bauer on Tue, 2013-10-29 16:54

Don't try this at home. Use the "realname" module and not this custom code.

Submitted by Tim on Tue, 2013-10-29 17:06

Sure, you can use that module if its feature set suits you and you're ok with its overhead, but if you need something simple, this token works just fine.

Submitted by Marc on Tue, 2013-10-29 19:27

You missed a lot. If you start formatting the username with firstname and lastname you will run into a large number of issues already solved in realname. Look into realname module and you may learn a bit and also that this has not more overhead than absolutly required. Reinventing the wheel is just dumb. Don't spread fear about realname module and overhead that does not exists, please.

Submitted by Tim on Tue, 2013-10-29 21:50

Sorry, but you didn't understand me. I'm not suggesting to use custom code to do what realname module does. I'm not talking about replacing username with First / Last name at all. This was just an example of the situation when you or some other module may overwrite username using hook hook_username_alter() and how you can handle this when you need a clean username. Once again, this is not about "overwriting" username, which is handled by realname module, this is about getting a raw username when you need it.

Submitted by Marc Bauer on Tue, 2013-10-29 23:22

But you only need the raw username if you alter the username. Otherwise there is no need for this. Once realname is enabled you also have this raw token.

Submitted by Tim on Wed, 2013-10-30 00:34

Marc, not only you can alter the username, there are third party modules that do use hook_username_alter() hook and alter username for their own purposes.

So for this case, when 3rd party module tweaks the username, I prefer two have these two functions in my glue module instead of installing yet another module. Every case has its own solution, that's all. Thanks for discussing this with me.

Submitted by albert_droit on Mon, 2014-03-24 22:56

Tim, thanks for the post and the custom module. Everyone can have an opinion, but mine is that in general if you want something simple and avoid performance, updating and maintenance costs, you better use simple yet generic modules. Your solution can be used for this specific functionality of the full name, but also it is a good example for extending it with similar functionalities for other tokens.


Add new comment

You are here