Guest User

Untitled

a guest
Jun 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /**
  2. * Implements hook_username_alter().
  3. * The main role is considered to be the one with the higher RID.
  4. */
  5. function username_prefix_username_alter(&$name, $account) {
  6. global $user;
  7.  
  8. // Retrieve user roles to understand the correct order.
  9. $site_roles = user_roles();
  10. if (!empty($account->roles)) {
  11. $user_roles = $account->roles;
  12. } elseif($cached_roles = &drupal_static(__FUNCTION__)) {
  13. if (!empty($cached_roles[$account->uid])) {
  14. $user_roles = $cached_roles[$account->uid];
  15. }
  16. } else {
  17. global $user;
  18. if ($account->uid == $user->uid) {
  19. $user_roles = $user->roles;
  20. dpm($user_roles, 'ZZP');
  21. } else {
  22. $user_obj = user_load($account->uid);
  23. $cached_roles = array();
  24. $cached_roles[$account->uid] = $user_obj->roles;
  25. }
  26. }
  27. $site_roles = array_reverse($site_roles, TRUE);
  28.  
  29. // Goes through every site role, in order or importance, and check if the user has it.
  30. // Then check if there is a prefix for the specific role. If not, keep checking...
  31. foreach ($site_roles as $key => $role) {
  32. if (!empty($user->roles[$key])) {
  33. $selected_role = $role;
  34. $selected_role_key = $key;
  35. $role_prefix = variable_get('username_prefix_' . $selected_role_key, '');
  36. if (!empty($role_prefix)) {
  37. $name = $role_prefix . $name;
  38. break;
  39. }
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment