Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello!
I'm trying to add the role of the currently logged in user to the body class. I've been trying to use the Drupal 7 code provided here: http://grasmash.com/article/drupal-add-current-users-role-body-classes, but I'm not having much luck getting this to work in Omega. Added the following to my omega sub-theme template.php file:
function mythemename_preprocess_page(&$vars) {
if ($vars['user']) {
foreach($vars['user']->roles as $key => $role){
$vars['class'][] = 'role-' . drupal_html_class($role);
}
}
}
I've also tried changing $vars to $variables, but that didn't work. Any tips?
Thanks!
Comments
Comment #1
dadderley CreditAttribution: dadderley commentedI would like Omega to do this as well.
I tried the same code that you did as well.
I played with it for a bit but could not make it work.
This is probably overkill, but the Context module makes this really easy to do.
Install it.
Add a context.
Then add a condition -- choose "User Role" -- choose the user roles you want.
Then add a reaction -- choose "Theme HTML" -- now you can create the body class that you want applied for the roles that you have chosen.
Comment #2
guysaban CreditAttribution: guysaban commentedThis worked for me:
function themename_preprocess_html(&$vars) {
// ... there might be other stuff here ...
$body_classes = array($vars['classes_array']);
if ($vars['user']) {
foreach($vars['user']->roles as $key => $role){
$role_class = 'role-' . str_replace(' ', '-', $role);
$vars['classes_array'][] = $role_class;
}
}
}
Comment #3
roberskine CreditAttribution: roberskine commented@guysaban's solution worked for me using a subtheme for seven. Just have to call it in your pages.tpl.php file. eg
sevensubtheme_preprocess_html();
Comment #4
PCateNumbersUSA CreditAttribution: PCateNumbersUSA commentedWould love to see this integrated into omega4 as well.
Comment #5
fubhy CreditAttribution: fubhy commentedI'd consider a patch for this for Omega 4.
Comment #6
GiorgosKfor 3.1 I ended up using
the difference from #2 is
$vars['attributes_array']['class'][] = $role_class;
Comment #7
FranCarstens CreditAttribution: FranCarstens commentedCode in #6 works in Omega4 as well.
Comment #8
RgnYLDZ CreditAttribution: RgnYLDZ commentedFor me I got all roles added as class.
EDİT:
Ok, sorry for that.
The administrator role was assigned as admin role in drupal. So I thougt it is showing it as extra role.
Comment #9
guile2912 CreditAttribution: guile2912 commentedHere is the code for Zen.
Also, it uses the drupal_clean_css_identifier function to make sure we respect the CSS grammar
-?[_a-zA-Z]+[_a-zA-Z0-9-]*
Comment #10
frederico CreditAttribution: frederico as a volunteer commentedThanks guile2912! Your code worked for me.
Comment #11
imkartik CreditAttribution: imkartik at TATA Consultancy Services for Pfizer, Inc. commentedFor omega v3.x, use:
$variables['classes_array'][] = 'new-class';
Comment #12
Sanjay Chauhan CreditAttribution: Sanjay Chauhan commentedHere is the module to add classes on body tag based on role or pages.
Common Body Class