Skip to main content
added 121 characters in body
Source Link
Druvision
  • 2.5k
  • 1
  • 25
  • 38

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';
  if}
  elseif (is_array($user->roles) && 
          in_array('non authenticated user', array_values($user->roles))) {
    $usertype = 'individual';
  if}
  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)

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';
  if (is_array($user->roles) && in_array('non authenticated user', array_values($user->roles))) $usertype = 'individual';
  if (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)

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)

Source Link
Druvision
  • 2.5k
  • 1
  • 25
  • 38

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';
  if (is_array($user->roles) && in_array('non authenticated user', array_values($user->roles))) $usertype = 'individual';
  if (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)