Drupal - Display user picture/avatar

in

Q: How can I theme and display a user picture or avatar in a page, block or content template with PHP in a way that works with ImageCache and respects the default user picture?

A: One easy way to embed a user picture into your content is to create a user view to output the picture and then just embed this view into your content by calling views_embed_view() passing in the user id as an argument.

Recently, the web design I was working on for one of our Cork clients called for the insertion of user avatars directly into some page content. It seems that there are loads of ways of using PHP to embed a user picture, for example, suggestions and code snippets can be found here:

http://drupal.org/node/35731

However I found all of this quite confusing and it seems that you have to manually handle the display of the default user picture in the cases where user has not configured an avatar of their own. Also, I wanted to apply ImageCache presets to my user images and it seems that would require further coding....

So as my head was spinning, I decided to ditch all of this confusion and take a different tack. I hatched a plan to create a view which would take the target user id as an argument and would output just the user's picture. To display the view (and hence the user avatar) all I would have to do is call views_embed_view() passing in the user id.

The advantage of this approach is that Views will automatically display the default user picture (if necessary) and also allows you to easily apply an ImageCache preset to the picture before it's displayed.

(0) Create an ImageCache preset for the user avatar (if you want to user one that is...)

(1) Create a 'user' view

(2) Add a field - User: Picture

(3) Remove the field label and choose an ImageCache preset if required

(3) Add an Argument - User: Uid

(4) Test and Save your view - type a valid uid into the 'Live Preview' arguments box and hit the preview button, if you see a user picture of the right size you're in business!

Now you're ready to embed the view into your content. Let's say that you called your view 'user_picture', and that have the target user's user id ($uid), then all you have to do to display the picture is:

  print views_embed_view('user_picture', 'default', $uid);

Thankfully it worked like a treat for me and I don't have to worry about all those head-wrecking and sometimes contradictory PHP snippets!!

If you wanted to employ different ImageCache presets in different situations, then all would have to do is add a new 'display' to the view and override the picture field's ImageCache settings...

Rock on Views!

BAvetriextsic (not verified)
BAvetriextsic's picture
Thankfully it worked like a

Thankfully it worked like a treat for me and I don't have to worry about all those head-wrecking and sometimes contradictory PHP snippets!!

Stranger
Stranger's picture
imagecache preset

in step 3, i don't have the option Imagecache Preset in my field's configuration...

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use BBCode tags in the text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Pride on FacebookPride on TwitterPride on TwitterPride on Twitter

Join Our Newsletter

* indicates required

Irish Internet Association

finalist.png
phone-number.png