Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Monthly Archives: November 2009

Gravatar support in Drupal

0 Comments

If you’ve been using WordPress you probably know about Gravatar. Gravatar is an abbreviation for “Globally Recognized Avatar”. It’s a service which provides an avatar across sites. While Gravatar support is built-in in WordPress, it requires an external module to be installed in Drupal. Recently I had to use the module in a Drupal 5 instance.

The module provides a gravatar only for comments. You can have the module insert the gravatar into the comment body itself or have it provide a field in the comment object ($comment->gravatar) which can be used by the theme. It does this by using the hook_comment() .

In our case though, we needed it to show the gravatar instead of the user picture if the user had not uploaded any. This was very easy to do. The key function here is _gravatar_get_gravatar(). You can provide it any or all of these parameters – mail, default, size or rating. Mail is the email id of the user. Gravatar can supply a unique generated avatar in case the email id you send them is not that of a registered Gravatar user. This can be of many types – identicon, wavatar or monsterid. Which of these are what you want, is specified in the default parameter. The size is of course, the size. The gravatars uploaded come with a rating – G, PG, R or X. You can specify a maximum rating with the rating parameter. You can read more about the gravatar parameters.

You can pass whichever of these parameters you want to _gravatar_get_gravatar() and it will return to you the url of the gravatar. Now pass this to the theme_gravatar() function in the gravatar module.

$picture = theme('gravatar',$gravatar_url,$username,$url);

You have to do all of this in the theme_user_picture function of your theme. If your theme is something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could change it to something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (user_access('use gravatar', $account) && (!user_access('disable own gravatar', $account) || !isset($account->gravatar) || $account->gravatar)) {
      $picture = _gravatar_get_gravatar(array('mail' => $account->mail));
      return theme('gravatar',$picture,$account->name,'');
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could even rewrite the default theme_gravatar provided by the gravatar module with your own THEMENAME_gravatar theme function.

Filed under drupal
Nov 4, 2009