drupalfun.com
Override the default user pic in comments and forums.
Edit: I'd like to note that this is an Imagefield purist's way of doing things. My goal was to create a site that did not use image.module at all (node-less images) and I wanted my user pics to be the same format. If you are going to use image.module anyway, there are some helpful comments in this thread that you might want to look over.
Okay, this is long-winded but if you can stand a little reading... Here is how to override Drupal's default user picture with our nifty custom avatar in comments, forums, or wherever else your theme uses it. I got this code from this post by Gaara on Drupal.org. The explaination is mine, so mistakes in it are all mine also (if you see any, let me know). I'm not a php programmer, so I'm just doing the best I can.
This is best for a new site, as it won't pick up avatars already added (unless you had the token module set up for user images already).
To do this, you should have followed the Drupal 6: Ultimate Community Site Guide at least up to the point where you have completed adding the custom avatar imagefield to your content_profile, though you can do this on any site with little modification. You will need the Token module for this. I'm going to assume that you are familiar with opening and editing files in a text editor and that you have access to your Drupal installation folder, by FTP or other means. I am also going to assume that you use the traditional Drupal paths and keep your contrib modules/themes in the sites/all folder.
There are three parts to this: setting up the avatar file path, enabling content_profile fields to be used in a custom template, and finally, creating the custom user-picture.tpl.php template file for our theme.
First, install and enable the token module. Go edit your content_profile content type and select 'manage fields'. For your avatar field, click 'configure'. Expand the path settings. Here is where we need to use a token. The token [uid] provides the user number, which we'll need for our image replacement code. So enter the following in the path:
user_pictures/[uid]
Now if user 4 (for example) uploads an avatar, it will be found at files/user_pictures/4/imagename.jpg
Second, we need to allow the content_profile variables to be used in our template, because we'll be making a custom user-picture.tpl.php file. Go into the sites/all/modules/content_profile directory and open up the content_profile.module file in your text editor of choice (I like Notepad++). Scroll down to around the 550th line in the code. This is an array containing the templates already allowed to use content_profile fields. You are going to add the template name, user_picture, to the array here (without the .tpl.php extension and using an underscore instead of a dash). I chose to add it under the user_profile line. When you've done so, it should look like this:
<?php
function content_profile_theme_registry_alter(&$items) {
$templates = array_merge(array(
'author_pane',
'comment',
'node',
'page',
'search_result',
'username',
'user_profile',
'user_picture',
'user_signature',
), variable_get('content_profile_extra_templates', array()));
?>
Now that our content_profile variables are available to use, we can make our custom template. Go to your Drupal root and into the modules/user folder. Make a copy of the user-picture.tpl.php file and place it into your theme folder in sites/all/themes/your_theme. Now open the new file in your text editor. Down near the bottom of this short file, you'll see this bit of code:
<?php print $picture; ?>
We are going to replace this section with a handy snippet of code that the original poster came up with:
<?php
$pict = $content_profile->get_variable('profile', 'field_avatar');
$title = $content_profile->get_variable('profile', 'title');
print theme('imagecache','thumbnail', "user_pictures/".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title);
?>
Now, there are variables you might need to change here. I'll run down the list. Where it says 'profile', that is the name of the content_profile content type. Mine is the default name 'profile'. The 'field_avatar' is of course the name of your custom image field for your content_profile. Change as necessary. The 'title' bit grabs the profile name and prints it as an alt tag and title tag, which gives the image the profile name when you mouse over it or when no image is present.
The last, long line of code contains 'imagecache', which we do not need to change. Where it says 'thumbnail', enter the name of the ImageCache preset you want applied to your avatar. Remember, this is replacing the user picture site-wide, so you should probably choose one with a small size.
The next sections of the code are the path where that user's avatar is stored. Provided you used Token to set up your imagefield's path as I listed at the beginning (user_pictures/[uid]), you should not have to change this.
Now save your file. You will have to clear the theme registry to get Drupal to see the new template file. You can do this easily from your site by logging in and going to the themes area (admin/build/themes) and simply clicking the 'save configuration' button.
Now, make some test users, upload some new avatars for them in their profiles and use them to post some comments.You should now see your custom avatars in comments and forums.
Well, that's it! Hope I made it clear enough. Like I said, if I made any mistakes, please let me know.
Additional notes: I had to go into the settings for Author Pane and Advanced Forum and remove the ImageCache preset to get the avatars to show up. The preset is already applied to our avatar field settings, and using it twice apparently makes it disappear.
Also, don't forget to assign the proper user permissions for ImageCache so your viewers can actually view your avatars.
Related Questions
Comments
2. Thank you, this is exactly
Thank you, this is exactly what I was looking for. I have it all up and running but have a question. I think my theme (pixture reloaded) posts the imagecache on the account page. I do not want it to appear there. How do I remove this?
3. My theme's crashing. I
My theme's crashing. I reverted and double checked everything, then re-applied your solution. I'm pretty much a white-belt when it comes to Drupal themes and PHP. I'm using a custom theme (Stingray from ThemeShark). Anyone know where I should start looking for the problem? Error message is:
Fatal error: allowed memory size of (128M) exhausted (tried to allocate 40961 bytes in /var/www/drupal/includes/theme.inc.
It doesn't like the PHP code we inserted in user-picture.tpl.php in ".../themes/your_theme" (/themes/stingray in my case). All my variables look correct. My content type is 'profile' and my image field is 'field_avatar' just like in Dorien's great eBook.
Any help appreciated!
4. That's not a bug. It's you
That's not a bug. It's you PHP memory limit that has been reached. See here: http://drupal.org/node/207036
5. Dorien, Thanks for your
Dorien, Thanks for your response!
That was my thought as well. Unfortunately not the problem. Even though Dev logs showed an average of 30M used before this code was added, I increased my PHP limit to 96M, than to 128M, and finally to 256M. In every case it used up all available PHP memory. If I comment out the 2 lines in user-picture.tpl.php (added to /sites/all/themes/my_theme) starting with "$pict = $content_profile...", and "$title = $ content_profile..." then no memory overrun. But if either of these is executed, my PHP memory is overrun. I spent a couple of days looking for some obvious conflict in my theme, but nothing makes sense.
So, I've removed these changes and have added the user pic with imagecache_profiles. This works, but not as clean a solution. See my next post on that topic.
6. If had had known this level
If had had known this level of finagling would be required to get CCK photos to show up on advanced forum comments, I would have used the default user picture! There has GOT to be a better way.
If anyone has written a helper module to substitute a CCK profile photo for the default user profile photo for author_pane, please say so.
I'm going to edit the author pane view directly, but that's hacking the module.
-Bram
7. Ive tried ever variation of
Ive tried ever variation of this possible.
I have no problem when the user has a uploaded image (used through the avatar by following the guide).
The problem is that the default image wont load. Using firebug I see that registered users are pointing to their unique folder (where their imagecache preset image would go if they had uploaded one)
Any ideas what is overriding the 'else' statement?


1. Giving a default avatar to
Giving a default avatar to users with no uploaded picture.
One thing I value a lot is visual consistency. So, I did not like the fact that users who have no uploaded avatar only have the profile name showing in place of the image.
I asked around on the Drupal.org forums about adding a default avatar to the script for those users who don't upload an image. They helped me work it out. :-)
Here's the improved code:
<?php
$pict = $content_profile->get_variable('profile', 'field_avatar');
$title = $content_profile->get_variable('profile', 'title');
if (is_array($pict[0])) {
print theme('imagecache','thumbnail', "user_pictures/".check_plain($account->uid)."/".$pict[0]['filename'], $title, $title);
}
else {
print '<img src="sites/default/files/default-user.png" alt="" />';
}
?>
Now the script will check for the presence of an avatar and serve up the image of our choice if it isn't present. I had a pre-formatted image, so I simply used the path to it (change this as needed for your own site and image file):
else {
print '<img src="sites/default/files/default-user.png" alt="" />';
}
Though you could just as easily set it up for ImageCache processing your default image:
else {
print theme('imagecache','thumbnail', "sites/default/files/default-user.png", $title, $title);
}
This will serve up the default image in forums and comments, on the account page, or wherever else our user-picture template is overriding.
However, if you're not using the view/panel combo from Ultimate Community Guide (where the view's 'empty text' returns a default avatar), you'll have to do one more thing to serve up a default within the actual user profile. Use the default image setting for your avatar imagefield instead. This is located in the field configuration settings, just above the 'Global settings' area.