Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_username_alter().
- * The main role is considered to be the one with the higher RID.
- */
- function username_prefix_username_alter(&$name, $account) {
- global $user;
- // Retrieve user roles to understand the correct order.
- $site_roles = user_roles();
- if (!empty($account->roles)) {
- $user_roles = $account->roles;
- } elseif($cached_roles = &drupal_static(__FUNCTION__)) {
- if (!empty($cached_roles[$account->uid])) {
- $user_roles = $cached_roles[$account->uid];
- }
- } else {
- global $user;
- if ($account->uid == $user->uid) {
- $user_roles = $user->roles;
- dpm($user_roles, 'ZZP');
- } else {
- $user_obj = user_load($account->uid);
- $cached_roles = array();
- $cached_roles[$account->uid] = $user_obj->roles;
- }
- }
- $site_roles = array_reverse($site_roles, TRUE);
- // Goes through every site role, in order or importance, and check if the user has it.
- // Then check if there is a prefix for the specific role. If not, keep checking...
- foreach ($site_roles as $key => $role) {
- if (!empty($user->roles[$key])) {
- $selected_role = $role;
- $selected_role_key = $key;
- $role_prefix = variable_get('username_prefix_' . $selected_role_key, '');
- if (!empty($role_prefix)) {
- $name = $role_prefix . $name;
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment