I'm trying to print a user's picture without the link to the user's profile which is what theme('user_picture', array('account' =>$user_item)) will render. If I use image_style_url('thumbnail', $user->picture->uri) the Gravatar hook will not be called.

So far the only way I have found to do this is:

if (function_exists('_gravatar_get_account_user_picture')) {
    $picture = _gravatar_get_account_user_picture($user);
} else {
    $picture = $user->picture->uri;
}
if (file_valid_uri($picture)) {
    $picture = image_style_url('thumbnail', $picture);
}

But this isn't very elegant, especially since I am developing a module and I'd rather not have to explicitly call the Gravatar module.

Any suggestions?

Thanks!

link|improve this question

feedback

2 Answers

up vote 3 down vote accepted
+50

The reason calling theme('user_picture') returns the user picture as a link is because it's hard coded into the gravatar_preprocess_user_picture() function (see lines 148 and 156 of the gravatar.module file).

This function uses _gravatar_get_account_user_picture() itself so your options are:

  • Use that function directly as you're doing at the moment
  • Hack the gravatar_preprocess_user_picture() function to return the <img> tag without the link
  • Perform some sort of hideous regex on the rendered HTML in your own preprocess function to extract the <img> tag

Personally I'd say the first option is the most desirable.

A slightly more 'Drupal' way to do it might be to use module_exists() instead of checking for the existence of the function itself. It returns TRUE:

if the module is both installed and enabled

While that's not ideal it's a tiny bit more elegant, and if you're worried about the length of code you could shorten it a bit:

$uri = module_exists('gravatar') ? _gravatar_get_account_user_picture($user) : $user->picture->uri

if (file_valid_uri($uri)) {
  $options = array('style_name' => 'thumbnail', 'uri' => $uri, 'alt' => 'Alt');
  $img_html = theme('image_style', $options);
}

The above code generates the HTML for the <img> tag directly using theme_image_style() without having to get the URI directly which I think is what you're after.

link|improve this answer
I'm still really surprised that there's no other way to do this; I mean Drupal does it somewhere internally. I'm pretty reluctant on this because I'm afraid of another module coming along and complicating the issue further, for example if a facebook avatar module were to be used. I really don't like having to program for every exception possible and update it every time a new module comes out (not that there's going to be a lot that do this). – Godwin Feb 17 at 5:44
This is a shortcoming of the Gravatar module, not Drupal in general to be honest. It would be very easy to add an admin settings page that allowed you to choose how to output the image (with or without the link) but the developers have decided not to do that. Even in the latest dev build they haven't included this option so if I'm afraid if you want it you'll have to hack the module to make it better, or work around it as you're already doing – Clive Feb 17 at 11:46
@Clive or instead of hacking the module... implement the feature and submit a patch to the module maintainers so others can use it too – Chaulky Feb 17 at 19:11
1  
@Chaulky Yep that'd be even better – Clive Feb 17 at 19:14
feedback

Obviously, handling directly in the Gravatar module would be best, but another option (though slightly hackish) would be to simply

$img = strip_tags(theme("user_picture", array('account' => $user_item)), "<img>");
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.