1

This is a Drupal 7 site. I have a profile2 module installed with several profile types for different users. The user name should be displayed according to the profile type:

  • For personal users - the user name should be build from the first and last name fields.
  • For business users, the user name should be taken from the business name field.

How do I display user names programmatically?

1

I've put the following code on a module:

<?
// Find the type of the current user
function mymodule_get_usertype(&$user) {
  $usertype = 'other';
  if (is_array($user->roles) && 
      in_array('authenticated user', array_values($user->roles))) {
    $usertype = 'individual';
  }
  elseif (is_array($user->roles) && 
          in_array('non authenticated user', array_values($user->roles))) {
    $usertype = 'individual';
  }
  elseif (is_array($user->roles) && 
          in_array('business', array_values($user->roles))) {
    $usertype = 'business';
  }
  return $usertype;
}

// Implements hook_username_alter,
// to display the user name according to the profile type
function mymodule_username_alter(&$name, $account) {

  $profile_type = mymodule_get_usertype($account);
  if (!empty($profile_type)) :
    $profiledata = profile2_by_uid_load($account->uid, $profile_type);
  endif;

  if ($profiledata) :

    switch ($profile_type) {
      case 'individual':
        $firstname = field_get_items('profile2', $profiledata, 'field_first_name');
        $firstname = $firstname[0]['safe_value'];
        $lastname = field_get_items('profile2', $profiledata, 'field_last_name');
        $lastname = $lastname[0]['safe_value'];
        $name = "$firstname $lastname" ;
        break;

      case 'business':
        $bizname = field_get_items('profile2', $profiledata, 'field_biz_name');
        $bizname = $bizname[0]['safe_value'];
        $name = $bizname;
        break;

      default:
        // For admin users, no change
    }
  endif;
}
?>

The result is that the new name is displayed whenever a user name is displayed (e.g. on the menus, on the user page, or on the user listing pages)

  • 1
    The method chosen is correct but I would recommend checking the for empty results from field_get_items() instead of assuming there is always going to be a value. The $user parameter doesn't need to be a reference in mymodule_get_usertype(). I would use elseif for the role checking to reduce unnecessary code running. - It is also good to keep with the drupal coding standards when providing help to try to promote good practice. drupal.org/coding-standards – rooby Oct 13 '13 at 13:23
  • Thanks for the code review. Question is if I can directly get the values without using the "$firstname[0]['safe_value'];" syntax. – Druvision Oct 13 '13 at 13:35
  • Nope there is no way around that. – rooby Oct 13 '13 at 13:42

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.