0

My goal is to attach a users role as a class to their user name anywhere it is displayed so I can color the link based on role.

I can get roles of every displayed user link using MYTHEME_username_alter:

function MYTHEME_username_alter(&$name, $account) {
  $user = user_load($account->uid);
  // I can get the roles of each user link through $user
}
  1. Is this the most efficient way to get the role for each user link?
  2. What is the best way to go about adding the role as a class to every user link?
  3. Am I way off track and overlooking an easier way to complete my goal?
0

2 Answers 2

1

I don't think you can do it with username_alter. You can only modify the user name string in there. Its probably better to use preprocess. In your theme template.php. Do something like this:

function YOUR_THEME_NAME_preprocess_username(&$variables) {
  // Username is linked, use the link_attributes to inject the class or any other attribute
  if (isset($variables['link_path'])) {
    $variables['link_attributes']['class'][] = 'SOME_CLASS';
  }
  // Username is NOT linked, maybe do something else or nothing
  else {
    $variables['attributes_array']['class'][] = 'SOME_CLASS';
  }
}
1
  • I kept running into preprocess_user when looking for a solution, thanks for pointing me at the correct preprocess. I figured out a solution using this function, I'll post it since it may help others but I'll accept this as the answer Feb 27, 2014 at 5:18
1

To add roles as a class to all user links I used the following:

function MYTHEME_preprocess_username(&$variables) {
  $user = user_load($variables['account']->uid);

  foreach ($user->roles as $role) {
    if (isset($variables['name'])) {
      $variables['attributes_array']['class'][] = 'role-' . strtolower(str_replace(' ', '-', $role));
    }
  }
}
6
  • You need not do user_load. Use global $user and you will have the $user object.
    – awm
    Feb 27, 2014 at 5:58
  • @arsenelupin That would give every user link the class of the currently logged in user Feb 27, 2014 at 6:38
  • Global user and user_load are identical. You are doing an extra load which hurts perfirmance . every time you do a load on the theme layer you nay be doing something wrong.
    – awm
    Feb 27, 2014 at 15:31
  • Pretend I have a block with 10 different users listed, the goal was to color each user based on their role. global $user gives you the currently logged in user's info. The code above is loading the role for each different user name as it is loaded. This can't be done with global $user. Feb 27, 2014 at 17:33
  • I see well every case is a little different. In any case,I would avoid doing user_load esp if you have listing of user names/ids. You should hook into whatever mechanism you use to generate this list, and add the role to its theme variables and then use the vars in your preprocess. Anyway, this is out of the scope of the question.
    – awm
    Feb 27, 2014 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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