I'm using a pattern for the username creation using the contrib module: automatic_usernames, this results in the following pattern for my use case: [hotel_name][uid]. Example: Hilton10

Here is the dilemma. This is fine for the business to see, but I do not want them to see their customer's actual username that they just created such as Hilton11 and instead I would like them to see the first name + last name of the customer.

How would I go about this using the following custom fields?

[#field_name] => field_customer_first_name

[#field_name] => field_customer_last_name

function MyCustomModule_username_alter(&$name, $account){
 global $user;
    if(in_array('venue admin', $user->roles)) {
    $name = ? //this is where im stuck

The $account argument in hook_username_alter() provides the user entity object with all its field values. You can use its field values for your $name substitution. For example:

function MyCustomModule_username_alter(&$name, $account){
  global $user;

  // This example presumes a field_first_name as a user first name field.
  // It will vary based on your field & language environment.
  // use Devel's dpm() to find your actual value for user entity.
  if (isset($account->field_first_name[LANGUAGE_NONE][0]['value']) 
      && in_array('venue admin', $user->roles)) {
    $name = $account->field_first_name[LANGUAGE_NONE][0]['value'];
  • Thank you for the help...since I had two fields that I wanted to concatenate I did the following. function MyCustomModule_username_alter(&$name, $account){ global $user; if(isset($account->field_customer_first_name[LANGUAGE_NONE][0]['value']) && $account->field_customer_last_name[LANGUAGE_NONE][0]['value']) { $name = $account->field_customer_first_name[LANGUAGE_NONE][0]['value'] . ' ' . $account->field_customer_last_name[LANGUAGE_NONE][0]['value']; } } – MRC Oct 17 '16 at 19:22
  • that's not 8.x code, if you are using Drupal 7, you should not use the "8" tag. – Berdir Oct 23 '16 at 12:23
  • @MRC's original question linked to a 7.x-only module so I'm presuming that was the case. I've changed the tag accordingly. – Shawn Conn Oct 23 '16 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.