Lars J wrote me asking how to display points with every user's info on all pages.
This article is the answer to this question.
Prerequisites
Before you can use this method, you have to be using the latest user points module.
You must have a version that supports the new call.
//$Id: userpoints.module,v 1.21 2005/10/30 02:23:02 kbahey Exp $
The version should be 1.21 or later.
Method
Basically, Drupal has a central function for displaying user info. This function is called format_name() in the file includes/common.inc . So the solution relies on changing that function to include the number of points with the user name.
As always, if you are not comfortable editing program files, then do not do it.
The function reads partially like this:
function format_name($object) {
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (strlen($object->name) > 20) {
$name = truncate_utf8($object->name, 15) .'...';
}
else {
$name = $object->name;
}
if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = $name;
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = '<a href="'. $object->homepage .'">'. $object->name .'</a>';
}
else {
$output = $object->name;
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
What needs to be done is making it look like this:
function format_name($object) {
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (strlen($object->name) > 20) {
$name = truncate_utf8($object->name, 15) .'...';
}
else {
$name = $object->name;
}
if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = $name;
}
if (module_exist('userpoints')) {
$output .= '(' . userpoints_get_current_points($object->uid) . ')';
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = '<a href="'. $object->homepage .'">'. $object->name .'</a>';
}
else {
$output = $object->name;
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
The only lines we added are the following:
if (module_exist('userpoints')) {
$output .= '(' . userpoints_get_current_points($object->uid) . ')';
}
These will cause every instance of users displayed on the site to have the number of points they have in brackets following the name.
You can add a div to the output so you can theme the points anyway you like with CSS in your stylesheets.
Enjoy ...
Comments
Lars Jansen (not verified)
Showing "0"
Fri, 2005/12/02 - 21:12Hi there once more Khalid,
Firstly, thanks for this article. It not only demonstrates a great new trick but also gives me ideas for other things I can do.
It turns out I was using an older version of the module, hence the errors I was recieving.
I still have a problem though. If I add
to my page.tpl.php it always prints "0" as my number of points even though I have 22 points in actual fact. (the points block confirms this) Even if I log in as another user I still get 0 points.
Any ideas why this might be?
Lars
Khalid
No $uid
Fri, 2005/12/02 - 22:45Yes, the $uid variable is not initialized to anything, and hence the query returns zero.
To solve this, you can use:
However, this will print the points for the current logged in user, and not every user.
Not sure if that is what you want ...
Lars Jansen (not verified)
Thats perfect!
Sun, 2005/12/04 - 00:34Khalid,
Thats PERFECT! Just what I was after, thank you so much for your time with this.
Using your example above I can start to build the member welcome on my pages. Like this:
"Welcome back 'username', you have (x) points."
Then I will use a if/else php thingy so that a non-logged in user see's a "register" link instead!
Awesome stuff....I love PHP:)
Thanks again Khalid!
Khalid
My pleasure
Sun, 2005/12/04 - 00:53You are welcome ...
Johnie (not verified)
Not working for me.
Fri, 2009/02/27 - 18:25Hello, I have tried this code, and for some reason it is still not working on my site.
This is the 5th post that I have read.
Can anyone help me out?
Thanks!