I'd like to be able to change the background color for comments posted by certain user roles. i.e: if a moderator makes a comment on a node, the CSS of the comment background would change appropriately. How do i access the User's Role variable and where do i place the code that calls the appropriate CSS style?

So far i have found a snippet that displays the user's role, but this seems to only work in the User Profile page and requires

<?php if (in_array('Girl Next Door',$user->roles)) {
print "Rollsonline Official Girl Next Door";
}
?>

Apparently this snippet requires the following to work as well<?php global $user; ?>

any hints as to how I can be able to show a different comment background color as per user role?

Comments

roleychiu’s picture

to clarify: i'd like to change the comment background color based on the AUTHOR'S role. If the author is of a specific role, then display his or her comment background differently than the other comments in the node.

- Roley Chiu
www.rollsonline.com

mlncn’s picture

Does anyone know an answer to this good question? There should be an easy way to display comments differently by user role... if not, I'll go looking for a hard way.

~ben

People Who Give a Damn :: http://pwgd.org/ :: Building the infrastructure of a network for everyone
Agaric Design Collective :: http://AgaricDesign.com/ :: Open Source Web Development

benjamin, Agaric

sepeck’s picture

I believe the Zen theme has an example of this. It's certainly possible.

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

mlncn’s picture

I'm actually basing off the Zen theme. (Although, pretty hacked up now)

I've got comment.tpl.php, and there's an $author variable, but reverse-engineering that to get the uid hardly makes any sense.

Aha... the $comment object variable is a wealth of (undocumented?) information:

[cid] => 1
[pid] => 0
[nid] => 1
[subject] => Comment Subject
[comment] => Comment text.
[format] => 1
[timestamp] => 1177539594
[name] => username
[mail] =>
[homepage] =>
[uid] => 1
[registered_name] => username
[picture] =>
[data] => a:0:{}
[score] => 0
[users] => a:1:{i:0;i:0;}
[thread] => 01/
[status] => 0
[depth] => 0
[new] => 0

As far as I can tell these are default variables, not added specially by Zen's template.php. Zen's template.php does have a good example of adding a class to a comment based on user ID (in this case, if a comment's uid is the same as the node's uid).

If anyone needs help beyond this, Agaric can certainly report back further.

~ben

Agaric Design Collective :: http://AgaricDesign.com/ :: Open Source Web Development

benjamin, Agaric

FableForge’s picture

I added the following lines to comment.tpl.php right after "print $submitted", for my particular theme:

$poster = user_load(array('uid' => $comment->uid));
$commentorRoles=(implode(", ", $poster->roles));
$commentorRoles = str_replace('authenticated user', 'Member', $commentorRoles);
print $commentorRoles;

It doesnt exactly change the background color, but I'm sure you could play with html or CSS to achieve that. Good luck!