I’m writing this down here quickly because it strikes me that it may well come in handy to others. This little module snippet that will add classes to Drupal’s html (On the body element) to indicate the current user’s role, and user ID.
Before:
After:
Handy if for example you want to style things differently based on the user’s role. Just drop it into your module, and replace MODULE with your module name.
The code:
function MODULE_preprocess_html ( &$variables ) { global $user; foreach ( $user->roles as $role_id => $role ) { $variables['classes_array'][] = "role-id-".$role_id; $variables['classes_array'][] = "role-".strtolower(drupal_clean_css_identifier($role)); } $variables['classes_array'][] = "user-uid-".$user->uid; }
June 5, 2013 at 7:55 am
Thank you! Works great! 🙂
June 24, 2013 at 4:44 am
Also check out https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_html_class/7
Just a wrapper for this function, but it converts alpha characters to lower case too.
June 24, 2013 at 4:45 am
*a wrapper for the “drupal_clean_css_identifier” function I meant to say
May 16, 2016 at 11:40 pm
This worked like a charm for me. I had to hide some fields from all others that were not admins and didn’t want to get too far in the weeds with access hooks and what not.
Used CSS.