0

Gravatar has a description of its php implementation on the following page:

https://en.gravatar.com/site/implement/images/php/

I'm trying to use this code to implement it in Drupal's user profiles and user pictures.

I have created a preprocess function to enable printing e-mail addresses in user-profile.tpl.php

function THEMENAME_preprocess_user_profile(&$variables) {
    $account = $variables['elements']['#account'];
    foreach (element_children($variables['elements']) as $key) {
        $variables['user_profile'][$key] = $variables['elements'][$key];
    }
    $variables['user_profile']['mail'] = $account->mail;
    field_attach_preprocess('user', $account, $variables['elements'], $variables);
}

Added the code to user-profile.tpl.php

print render($user_profile['mail']);

This code works what it's supposed to do - it displays mail address in the user profile. Now I need to use that address to create gravatars in profiles and in user pictures later.

I have somehow tried to connect the tutorial on Gravatar's site and this code, but without success. Here is the code (I have tried at least 20 different combinations):

$email = "['user_profile']['mail']";
$default = "http://www.somewhere.com/homestar.jpg";
$size = 40;
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
        $url = 'http://www.gravatar.com/avatar/';
        $url .= md5( strtolower( trim( $email ) ) );
        $url .= "?s=$s&d=$d&r=$r";
        if ( $img ) {
            $url = '<img src="' . $url . '"';
            foreach ( $atts as $key => $val )
                $url .= ' ' . $key . '="' . $val . '"';
            $url .= ' />';
        }
        return $url;
    }

As far as I understand, the problem is in this row:

$email = "['user_profile']['mail']";

What am I doing wrong and what is the right expression to put between quotes in this row?

Jake McGraw
  • 55,558
  • 10
  • 50
  • 63
take2
  • 616
  • 2
  • 17
  • 31
  • 1
    What does that have to do with this question? I have accepted every answer that I managed to implement, i. e. that solved my problem. – take2 Oct 13 '12 at 17:01

1 Answers1

2

The question is about basic PHP syntax, $variables['user_profile']['mail'] is used a simple access to nested arrays (ie. an array inside an array. In addition, if print render($user_profile['mail']); displays the user email address, $user_profile['mail'] is obviously the email address to pass to your custom get_gravatar() function.

$variables['user_profile']['gravater_url'] = get_gravatar($user_profile['mail']); 

I would recommend using the Gravatar Integration module instead of writing your own implementation.

Jake McGraw
  • 55,558
  • 10
  • 50
  • 63
Pierre Buyle
  • 4,883
  • 2
  • 32
  • 31