Hi,

Is there a module or easy way to assign a color to a role/group/rank so that all members of that group or rank have a special colored username?

Comments

WorldFallz’s picture

No module that I know of, but you can do it pretty easily. Override the theme_username function in your template.php file and style however you wish.

NeoID’s picture

That would mean I'll have to hardcode either the username or role to a specific color.. not a very good solution... :/

liliplanet’s picture

There is a new feature at http://drupal.org/project/realname where you can add an image to each role ..

see http://drupal.org/node/388928

WorldFallz’s picture

I'm not sure I understand-- do you want the user to be able to change the color or something?

NeoID’s picture

I want to be able to do the same as you can on every other modern forum; create a group, add members and (as an admin) give certain groups a special colored username...

WorldFallz’s picture

Maybe http://drupal.org/project/user_badges is close to what you want? Otherwise, I still think theme_username is the way to go-- you can put any php you want there, so you can assign classes based on user roles then style/color them in css any way you like. You can also limit it to forum posts or let it follow the user throughout the site.

NeoID’s picture

I've figured out that the below code works, but I'm trying to clean it up a bit and add a switch/case in order to check for more user roles.
So... I want to check if the user role is Moderator and if yes add a div-wrapper "moderator-role", but if the role is for example "Event" it should add a div with a class called "event-role"...

Anyone who knows how to clean it up a bit, it looks really messy if I copy/paste it for every user-role I want to check...

<?php
 global $user;

  // Check to see if $user has the administrator role.
  if (in_array('Moderator', array_values($user->roles))) {
  
function phptemplate_username($object) {
  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }
    if (user_access('access user profiles')) {
      $output = '<div class="moderator-role">' . l($name, 'user/'. $object->uid, array('attributes' => array('title' => t('View user profile.')))) . '</div>';
    }
    else {
      $output = check_plain($name);

    }
  }
  return $output;
}
  
  }
?>
NeoID’s picture

I found a nice way to do this, however, instead of printing the role for the user currently logged in, how can I modify it in order to affect the current member (in the theme_username function)?

<?php
foreach ($user->roles as $role)
	{
	if ($role == "authenticated user") {
		$newrole = "role-";
	}
	else {
		$newrole = $role;
	}
	$roleClass .= t("%role", array('%role' => $newrole,));
	}
?>