1

I'm using the Facebook Connect Module on my Drupal 7 site. If a user logs onto my site via Facebook, his Facebook user picture gets imported to his account on my site. But now I am wondering whether or not there is any way to show this Facebook user picture in Views. This picture is only shown on the user profile page, not in any view that references the user picture field.

Does anybody know how to do this or is there any way to automatically import the Facebook picture to be the standard user picture?

2

I found a solution: You can use the following hook to copy facebook's user image and save it as drupal user image. Make sure to ask your users to allow this!

/* Copy facebook image if no user image is present.
   See: http://drupal.org/node/1764176 */
function custom_api_user_login (&$edit, $account) {
  $fbuid = fbconnect_get_fbuid();
  if(is_null($account->picture) && $fbuid && isset($account->data['fb_avatar']) && $account->data['fb_avatar']){
    #$test = fbconnect_user_picture_override(array('fbuid' => $fbuid, 'account' => $account, 'size' => '25'));
    #drupal_set_message(print_r($test, true), 'status', true);

    if(copy("http://graph.facebook.com/".$fbuid."/picture?type=normal", '/tmp/'.$fbuid.'.jpg')) {
      $path = '/tmp/'.$fbuid.'.jpg';
      $file = new StdClass();
      $file->uid = $account->uid;
      $file->uri = $path;
      $file->filename = $fbuid;
      $file->filemime = file_get_mimetype($file->uri);
      $file->filesize = filesize($path);
      $file->status = FILE_STATUS_PERMANENT;
      $dest = "public://" . $fbuid.'.jpg';
      $file = file_copy($file, $dest);
      file_save($file);
      $edit['picture'] = $file;
      user_save($account, $edit);
      #drupal_set_message(print_r('<img typeof="foaf:Image" src="'.$path.'" alt="Naomi Yoshida">', true), 'status', true);
    } else {
      drupal_set_message(print_r('Kopieren schlug fehl', true), 'status', true);
    }
   }
}
0

other best place to hook

/**
 * hook_fboauth_user_save()
 * @param type $edit
 * @param type $fbuser
 * 
 */
function sapbrw_registration_fboauth_user_save($account, $fbuser) {

  // Check if the logged in user has a picture saved & has linked their FB     account through fboauth
  if (empty($account->picture)) {
if (copy("http://graph.facebook.com/" . $fbuid . "/picture?type=normal", '/tmp/' . $fbuser->id . '.jpg')) {
  $path = '/tmp/' . $fbuser->id . '.jpg';
  $file = new StdClass();
  $file->uid = $account->uid;
  $file->uri = $path;
  $file->filename = $fbuser->id;
  $file->filemime = file_get_mimetype($file->uri);
  $file->filesize = filesize($path);
  $file->status = FILE_STATUS_PERMANENT;
  $dest = "public://" . $fbuser->id . '.jpg';
  $file = file_copy($file, $dest);
  file_save($file);
  $edit['picture'] = $file;
  user_save($account, $edit);
}
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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