
If you'd like to change your site's appearance based on the current user's role, you can easily add the current user's role to the $body_classes array.
Just add this snippet to the yourTheme_preprocess_page function in your template.php file.
Drupal 6:
<?php
function grasmash_preprocess_page(&$vars, $hook) {
$body_classes = array($vars['body_classes']);
if ($vars['user']) {
foreach($vars['user']->roles as $key => $role){
$role_class = 'role-' . str_replace(' ', '-', $role);
$body_classes[] = $role_class;
}
}
$vars['body_classes'] = implode(' ', $body_classes); // Concatenate with spaces
}
?>
Thanks to Anders Iversen for a Drupal 7 Version:
<?php
function grasmash_preprocess_page(&$vars) {
if ($vars['user']) {
foreach($vars['user']->roles as $key => $role){
$vars['class'][] = 'role-' . drupal_html_class($role);
}
}
}
?>
You may also want to check out my post on adding body classes based on user permissions.
For Drupal 7
In drupal 7 things have changed a tiny bit.
You need to add it to the function ninesixty_preprocess_html instead:
function ninesixty_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;
}
}
}
Cheers :)
Thanks Anders. I've updated
In reply to For Drupal 7 by Anders Iversen
Thanks Anders. I've updated the article to include a Drupal 7 version.
Worked Great
Anders Iversen's solution for Drupal 7 worked great. I'm using the zen theme though and I had to change "vars" to "variables" in all instances.
I could never get this to
I could never get this to fully work in my theme, and found a viable alternative is to use context 3. I simply created one context for each role, works great.
Condition: User role (Select role(s) for which to set the context)
Reaction: Theme HTML (text in section class field will be printed in body class)
One limitation is that the
Contexts. This worked for me too
In reply to I could never get this to by synthetic
Same problem and same solution. Thanks.
good
In reply to Contexts. This worked for me too by Craig Tockman
This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
<a href="http://www.cgo-media.com/seo-manchester/">SEO Manchester</a>
One limitation is that the
One limitation is that the section class field is static text, though it worked just fine for my limited amount of roles & needs.
Great!
Thank you very much. For Drupal 7 it works perfectly!
admin pages
Anders Iversen's solution works great except for all admin pages (pages /admin/*).
Any idea of a possible workaround ...? Thanks.
good
In reply to admin pages by Remaye
Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
<a href="http://www.britanniasecuritygroup.com/">Roller Shutters</a>
admin pages
hum... sorry... of course it doesn't work : admin pages have a different theme !!
(shame on me)
Unfortunately, it didn't work
Unfortunately, it didn't work for me in D7. I tried both the code in the article and comment. The roles names aren't showing up. I cleared cache, theme registry, everything. :(
Version for Omega theme
This code will work with the Omega theme (which uses attributes_array['class'][] instead of classes_array[] :
if ($vars['user']) { foreach($vars['user']->roles as $key => $role){ $vars['attributes_array']['class'][] = 'role-' . drupal_html_class($role); } }
In case the code isn't working for you (D7)
Try this one (it helped me):
function THEMENAME_preprocess_html(&$vars) {
foreach($vars['user']->roles as $role){
$vars['classes_array'][] = 'role-' . drupal_html_class($role);
}
}
---
Source: https://drupal.org/node/1489460#comment-8632639
Stanley High School Online
Good post you have shared here Thanks a lot.
<a href="http://www.stanleyhighschool.com">High School Diploma Online</a>
Add new comment