Altering the username

Public

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
php
  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);
  7.  
  8. if (!empty($account->field_first_name) && !empty($account->field_last_name)) {
  9.  
  10. $name = $account->field_first_name['und'][0]['value'] . ' ' . $account->field_last_name['und'][0]['value'];
  11. }
  12. }