0

I have a cron hook to check the data of all users for a couple of reasons. One of them is to see if they have a user picture set. If not, i want to give them a default user picture, based on their gender.

I create this picture in the enable hook of my module, and i have a file id. However, when i try to set this on the user, it doesn't work. I've tried multiple things, like:

$picture = file_load(variable_get("default_userpic_$gender"));
$account->picture = $picture->fid;
user_save($account);

or

$picture = file_load(variable_get("default_userpic_$gender"));
$edit = array('picture' => $picture);
user_save($account, $edit);

and several variations along this theme. I've also tried to use a user_presave hook and add the picture to the edit there. However, when i check the user, the picture is still NULL. Does anyone know something that might cause this?

6
  • Have you confirmed that $picture is a valid object with a fid member?
    – Clive
    Jun 13, 2013 at 9:58
  • Yes, picture is a complete, working file.
    – Erik S
    Jun 13, 2013 at 10:42
  • Try using the 2nd example, but with $edit = array('picture' => $picture->fid); picture is an integer column, I think it'll get upset if you try to stuff a whole file object into it
    – Clive
    Jun 13, 2013 at 10:42
  • By the way, if i check the &$edit field in user_presave, i do see the picture in the edit. The user itself does not update when i check it in Drupal, however.
    – Erik S
    Jun 13, 2013 at 10:42
  • I'm quite certain I've tried that one as well, but I'll check again when I get home for good measure.
    – Erik S
    Jun 13, 2013 at 11:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.