1

I'd like to style the username of certain roles differently (mainly admin roles) and I think the way to do it is to append the user roles in the class of the username.

I'd like your help with this because I've tried this but it doesn't work:

function THEME_username(&$vars) {
  foreach($vars['user']->roles as $role){
    $vars['classes_array'][] = 'role-' . drupal_html_class($role);
  }
}
6
  • 2
    Keep in mind that you might not want to show some roles. If there is a role for comment moderation, for example, letting users know who, personally, moderates their comments might be a bad idea. So think twice if in your use case it is OK, or should you define an array of "classable" roles and omit ones not listed.
    – Mołot
    Jul 8, 2014 at 9:24
  • Thanks for raising the issue. I'm thinking that to solve the problem comment moderators could be labelled as something vaguer like admin2. What do you think?
    – ashash
    Jul 8, 2014 at 9:34
  • I think that it totally depends on your situation. I already wrote how I would approach that. But whatever works for you - as long as you know there may be an issue and design your solutions with this in mind, you'll be OK.
    – Mołot
    Jul 8, 2014 at 9:37
  • 1
    Thanks, I read your suggestion again and I understand the downsides of my approach in more detail now, since I have different layers of administration, being able to name my roles properly while hiding it is important. Now, I agree with wholeheartedly with your suggestion. I'm trying to implementing it with simple switch statements and we'll see how it goes.
    – ashash
    Jul 8, 2014 at 9:51
  • I tried this but it doesn't work: if(count(array_intersect($vars['account']->roles, $target)) > 0) {echo 'works'} returns an error "Warning: array_intersect() [function.array-intersect]: Argument #1 is not an array". However, is_array($vars['account']->roles) returns true. Could you help me with some suggestions please?
    – ashash
    Jul 8, 2014 at 19:53

2 Answers 2

2

You need to preprocess the theme function, and add to the attributes array or link options, depending on context. Currently you're overriding the theme function and reducing its output to an empty string.

function MYTHEME_preprocess_username(&$vars) {
  foreach($vars['account']->roles as $role) {
    if (isset($variables['link_path'])) {
      $vars['link_options']['attributes']['class'][] = 'role-' . drupal_html_class($role);
    }
    else {
      $vars['attributes_array']['class'][] = 'role-' . drupal_html_class($role);
    }
  }
}
1
  • Hmm... the roles do get added as the class, but upon retrying I find that it gives me an error: "Warning: Invalid argument supplied for foreach()" .
    – ashash
    Jul 8, 2014 at 19:37
1

Given the advice by Mołot, I've decided that this works best for me since I can hide the specific user roles a user has.

function MYTHEME_preprocess_username(&$vars) {
 $target = array('admin','admin 1','admin forum','admin shop');
  if(count(array_intersect((array)$vars['account']->roles, $target)) > 0) {
    if (isset($variables['link_path'])) {
      $vars['link_options']['attributes']['class'][] = 'role1';
    }
    else {
      $vars['attributes_array']['class'][] = 'role1';
    }
  }
 }

Clive has answered my question correctly and I'm leaving his answer as the accepted one, since it addressed my initial question.

Your Answer

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

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