0

I want to style comments differently depending of the author's user roles. How can i add a specific class attribute in the comment template if the author of the comment has a certain role?

2

I got it myself. Here is how to do it:

In your theme template.php:

function MYTHEME_preprocess_comment(&$vars) {
    // user roles
    // 1 = anonymous user
    // 2 = authenticated user
    // 3 = administrator
    // ...
    $vars['role_classes'] = '';
    $sql = "SELECT users_roles.rid FROM {users_roles} WHERE users_roles.uid = " . ((int) $vars['comment']->uid);
    $result = db_query($sql);
    foreach($result as $record) {
        $roleId = $record->rid;
        if($roleId == 3) $vars['role_classes'] .= ' comment-by-admin';
    }
}

Then change in your comment.tpl.php

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

to

<div class="<?php print $classes; ?><?php print $role_classes; ?> clearfix"<?php print $attributes; ?>>
0

It would probably be best to add an extra class to the body tag, so you can target .comment etc. inside the body.role-xxx - you can then use it on any element on the page

The site I'm on right now has

<body class="html not-front logged-in no-sidebars page-node page-node- page-node-521317 node-type-article" >

You need to add another to that by

global $user;
$custom = '';
foreach($user->roles as $key => $value ) {
  $custom .= ' role-' . $key; //or value, if you prefer 
}
// add the string to the body attributes in the link below

http://api.drupal.org/api/drupal/includes--theme.inc/function/template_preprocess_html/7

Sorry, that is a bit garbled as I've got to run out the door right this second - but hopefully you can decipher and get your answer ;o)

Edit:

Tested working solution for you (I was slightly off with my guess yesterday)

function THEMENAME_preprocess_html(&$variables) {
  global $user;
  foreach($user->roles as $key => $value) {
  $variables['classes_array'][] = 'role-' . $key;
}

Then you can do something like

body.role-3 .comment {
  border: 2px solid green
}
3
  • I am not sure if this is what i need, because the role is not page related, but comment related. I can have 10 comments on the same page with different authors which should be rendered dependent on the role of the comment author. – Alp Nov 22 '11 at 17:56
  • Yeh, sorry I misread the question (thinking you meant 'logged in user'). I would certainly recommend against doing a database query on every comment template preprocess though - that will not scale well at all if you have a lot of authenticated traffic... although I can't think of a proper solution. It might be that performance is not an issue for the type of site you are building? (Sometimes you do just have to wang in a DB query here and there to get Drupal to do what you want) – joevallender Nov 23 '11 at 9:03
  • You are right, i didn't thought of the performance issues of my solution. Shouldn't be a big problem at the moment, but i'd love to see another and better scaling option. – Alp Nov 23 '11 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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