Drupal Tip #10: Beautiful User Profile in Topics

A new article to celebrate Drupal 7 release!

As you probably read it in the previous article (I hope you did it!), you are now able to have beautiful forum topics, but they are still missing an important thing: the profile of the user who sent the message. Here is how to do it.

Great Drupal User Profile

Displaying a real block that describes the profile, and contains relevant information about the member who posted a message is really important. It indeed helps other users seeing who posted, and what kind of user could be linked to the post.

  1. We have created a function, called user_forum_load() that extends the classic loading of a Drupal user, to add interesting information related to the forum topic and the user.

     * @param long $uid
     * @param string $name for anonymous user, the name can be provided
     * @param string $mail for anonymous user, the mail address can be provided
    function user_forum_load($uid, $name = "", $mail = "") {
    	// Anonymous user
    	if ($uid == 0) {
    		$user = drupal_anonymous_user();
    		$user->name = $name;
    		$user->mail = $mail;
    		$user->profile_website = $comment->homepage;
    		$user->comment_count = 1;
    		$user->created = null;
    	// Registered user
    	else {
    		$user = user_load($uid);
    		// Init User Profile Fields. Useful if you have additional fields that customize the user account
    		$user->comment_count = get_user_comment_count($user);
    	$user->user_status = get_user_status($user);
    	$user->user_rank = get_user_rank($user);
    	$user->user_rank_label = get_user_rank_label($user->user_rank);
    	$user->user_rank_image = get_user_rank_image($user->user_rank);
    	return $user;
    function get_user_status($user) {
    	if ($user->uid == 0 || $user->roles[DRUPAL_ANONYMOUS_RID] == 'anonymous user') {
    		return "Anonymous User";
    	else if ($user->uid == 1) {
    		return "Administrator";
    	return "Registered User";
    function get_user_comment_count($user) {
    	$query = "
    		select c.total + n.total as total
    				(SELECT count(cid) as total FROM {comments} c WHERE c.uid = '%s') c,
    				(SELECT count(nid) as total FROM {node} n WHERE n.uid = '%s' and n.type = '%s') n
    	$result = db_query($query, $user->uid, $user->uid, 'forum');
    	$data = db_fetch_array($result);
    	return $data["total"];
    function get_user_rank($comment_count) {
    	if ($comment_count < 50) {
    		return RANK_NEWBIE;
    	} elseif ($comment_count < 200) {
    		return RANK_REGULAR;
    	} elseif ($comment_count < 1000) {
    		return RANK_PASSIONATED;
    	} elseif ($comment_count < 4000) {
    		return RANK_GURU;
    	return RANK_GOD;
    function get_user_rank_label($rank) {
    	$user_rank_labels = array(
    		RANK_NEWBIE => "FSR Newbie",
    		RANK_REGULAR => "FSR Regular",
    		RANK_PASSIONATED => "FSR Passionated",
    		RANK_GURU => "FSR Guru",
    		RANK_GOD => "FSR God",
    	return $user_rank_labels[$rank];
    function get_user_rank_image($rank) {
    	$o = "<span class='user-rank' title='" . get_user_rank_label($rank) . "'>";
    	for ($i = 0 ; $i < $rank; ++$i) {
    		$o .= "<img src='" . static_image_path() . "/user-rank.png' alt='Rank' />";
    	$o .= "</span>";
    	return $o;

    Several Comments:

    • Nothing extraordinary here, except that you can see that all these small functions can give you a full control on the way your are managing users, ranks and statuses. Of course, our usage is pretty basic, but it could easily be improved by getting them from the database and so on. Note that this is not the main purpose, as our main objective is to theme Drupal, not rewriting it from scratch.
  2. As usual, you have to call the function above to make it available for the concerned template files, using the preprocessors (that you can find in the template.php file of your theme folder).


    	function yourtheme_preprocess_comment(&$variables) {
    		$comment = $variables["comment"];
    		$user_forum = user_forum_load($comment->uid, $comment->name, $comment->mail);
    		$variables["user_forum"] = $user_forum;
    	function yourtheme_preprocess_node(&$variables) {
    		$node = $variables["node"];
    		// See node-forum.tpl.php
    		if ($node->type == "forum") {
    			$variables["user_forum"] = user_forum_load($node->uid);

    The available $user_forum variable is therefore usable by the user-forum-profile.tpl.php file we included in a previous example, in the files node-forum.tpl.php and comment-forum.tpl.php.

  3. And here is the missing file, that will display these information.


    <div class="user-forum-profile">
    	<?php print $picture ?>
    	<h4><?php print ucfirst($user_forum->name); ?></h4>
    	<div><?php print $user_forum->user_status; ?></div>
    	<div><?php print $user_forum->user_rank_image; ?></div>
    	<div class='posts'><strong>Posts:</strong> <?php print $user_forum->comment_count; ?></div>
    	<div class='joined'><strong>Joined:</strong> 
    			if ($user_forum->created != null)
    				print format_date($user_forum->created, "custom", 'D F j, Y  H:i');
    				print "<em>Never.</em>"; 
    	<div class='fan-of'><strong>Fan of:</strong> 
    			if ($user_forum->profile_favorite_forum_software_shortcut != null)
    				print "<a href='/$user_forum->profile_favorite_forum_software_shortcut/review' title='$user_forum->profile_favorite_forum_software Review'>$user_forum->profile_favorite_forum_software</a>";	
    				print "<em>-</em>";
    	<div class='contact'>
    		<a href="<?php print $user_forum->profile_website; ?>" title="<?php print ucfirst($user_forum->name); ?>'s Website"><img src="<?php print static_image_path() ?>/www.png" alt="WWW" /></a>
    		<a href="mailto:<?php print $user_forum->mail; ?>" title="Contact <?php print ucfirst($user_forum->name); ?>"><img src="<?php print static_image_path() ?>/email.png" alt="WWW" /></a>

  4. As usual, you need a bit of CSS theming to make this looks nice.

     * User Forum Profile
    .user-forum-profile {
     float:    right;
     padding:   0 0 0 10px;
     border-left:  1px solid #fff;
     width:    170px;
    .user-forum-profile .picture {
     float:     none;
     margin:    0; 
    .user-forum-profile h4 {
     margin:    0px; 
    .user-forum-profile .joined, 
    .user-forum-profile .fan-of {
     font-size:   11px;
     color:    #999;
    .user-forum-profile .posts {
     font-size:   11px;
    .user-forum-profile strong {
     color:    #555;
    .user-forum-profile .contact {
     margin:    5px 0 0 0; 
    .user-forum-profile .contact a {
     padding:   3px;
     opacity:   0.3;
    .user-forum-profile .contact a:hover {
     opacity:   1;
     border-bottom:   none;

Finally, your forum looks like any other, but it has a major advantages compared to them: it is fully integrated with Drupal! More over, it also demonstrates how easy it is to modify and improve the code base of this CMS.

Comments are closed.

Comments (8)

Hello, where to put the

Hello, where to put the Function? I don't know what i must do! Please help!



lastnico's picture

Hi, I suggest you to put all


I suggest you to put all these functions to the template.php file.

FSR Admin - Vote for 2012.


I benefited a lot from this

I benefited a lot from this article, thank you.



function get_user_comment_count() undefined?

lastnico's picture

That's right, it was missing

That's right, it was missing in the snippet, this is now fixed. Happy testing!

FSR Admin - Vote for 2012.


thank you, master!

thank you, master!


Hi! Great stuff! And

Hi! Great stuff! And furthermore almost everything works fine! Big smile Just have some questions, can't find the function profile_load_profile() anywhere in your code. Where could I get it from?

And, another one (last), would it be possible to get the complete "user-forum-profile.tpl.php" file? I think there are some (minor) bits and pieces missing on the code given above.

And, above all, thanks a lot for this!! Best regards.


This kind of stuff just

This kind of stuff just throws me for a loop. Thanks for the code, but what do I do with it?

  Top Top