blog-banner

Drupal User Picture Deleted Automatically

  • Drupal 7
  • Drupal Planet
  • Picture Removed
  • Profile Picture

Drupal Global User

 
Sometimes you could be in a fury when a user's picture gets deleted automatically with nothing being noticed as strange in Drupal. Even this thread 935592 might not help you. Then you have come to the right place. Of course, the culprit could be your call to user_save() somewhere. The actual issue might be, that you are passing a global user object instead of a full account object. The first param of user_save() should be a full account object, while global user does not have all the data of the account object. In this case, $account->picture is an object while $user->picture is just an integer, fid (File id) of the image file. So while trying to save, your picture associated with the user account gets broken. The reason can be understood by looking at the user_save() source code. The method checks for
empty($account->picture->fid)
While using $user, this condition becomes false (we only have $account->picture not $account->picture->fid) and user picuter is removed.
So make sure you call like this,
global $user
$account = user_load($user->uid);
/*Some operations with $account object*/
user_save($account, $edit); /*NOT user_save($user, $edit)*/
Reference: https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_save/7
Get awesome tech content in your inbox