2012-10-13 15 views
0

的Gravatar有PHP執行以下網頁上的說明:

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

我想使用此代碼在Drupal的用戶配置文件和用戶圖片中實現它。

我創建了一個預處理功能,能夠在用戶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); 
} 

添加的代碼到用戶profile.tpl.php

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

這段代碼的工作原理是它在用戶配置文件中顯示郵件地址。現在我需要使用該地址在以後的配置文件和用戶圖片中創建gravatars。

我不知何故嘗試連接Gravatar的網站和此代碼的教程,但沒有成功。這裏是代碼(我已經試過至少20個不同的組合):

$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; 
    } 

據我瞭解,問題是此行中:

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

我在做什麼錯的,什麼是在這一行中引號之間的正確表達式?

+1

是什麼有這個問題呢?我已經接受了每一個答案,我設法實現,即這解決了我的問題。 – take2

回答

2

的問題是關於基本PHP語法,$variables['user_profile']['mail']使用簡單訪問嵌套陣列(即陣列內的陣列。此外,如果print render($user_profile['mail']);顯示用戶的電子郵件地址,$user_profile['mail']顯然是要傳遞到的電子郵件地址您的自定義get_gravatar()功能。

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

我會建議使用Gravatar Integration module而不是寫自己的實現。

相關問題