Skip to main content
picture deleted automatically

Drupal user picture deleted automatically

Sometimes you could be in a fury when user picture gets deleted automatically with nothing being noticed as strange. Even this thread 935592 might not help you. Then you have come to right place. Ofcourse the culprit could be your call to user_save() somewhere. The actual issue might be, you are passing global user object instead of 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 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 association 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

Comments

Or even better, in stead of possibly introducing a security breach by making a mistake with the global $user variable. You can use user_uid_optional_load() which will return the fully loaded user account of the current user when called without a uid argument. This results in one line of code less and improved security.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.