Altering the username


Hey ho everybody and thank you for viewing my code Snipp.

In this snipp we altering the Username [Value] <- Only the value not the prefix or suffix
So now you think wait a sec why on earth should I do that ?

For example.
You have a page with costumers and your page should be a bit more seriusly.
So all you have to do is altering the username [Value] and replace it with fields like first name and last name.

Get raw version
  1. /**
  2.  * Altering the username with the first and lastname of the User
  3.  */
  4. function hook_username_alter(&$name, $account) {
  5. // With user_load we get all fields with are added not by core and add them to the orginal $account
  6. $account = user_load($account->uid);
  8. if (!empty($account->field_first_name) && !empty($account->field_last_name)) {
  10. $name = $account->field_first_name['und'][0]['value'] . ' ' . $account->field_last_name['und'][0]['value'];
  11. }
  12. }