0

When trying to update user (custom) fields programatically in drupal 7, I get the weird problem that whenever I do, my password is reset, my user is logged out, and I have to reset password to be able to log-in again.

I have tried a few different things, none seem to work, here's my code.

global $user;
global $language;
$fullUser = user_load($user->uid);

$updateUserParams = array();

$updateUserParams['language'] = $language->language;
$updateUserParams['field_first_name'][LANGUAGE_NONE][0]['value'] = $form_state['values']['firstName'];
$updateUserParams['field_surname'][LANGUAGE_NONE][0]['value'] = $form_state['values']['lastName'];

$year = $form_state['values']['birthdate']['year'];
$month = $form_state['values']['birthdate']['month'];
$day = $form_state['values']['birthdate']['day'];

$birthdate = date( "Y-m-d", strtotime($year.'-'.$month.'-'.$day));

$updateUserParams['field_birthdate'][LANGUAGE_NONE][0]['value'] = $birthdate;
$updateUserParams['gender'][LANGUAGE_NONE][0]['value'] = $form_state['values']['gender'];
$updateUserParams['field_address']['en'][0]['thoroughfare'] = $form_state['values']['street'];
$updateUserParams['field_address']['en'][0]['postal_code'] = $form_state['values']['postalCode'];
$updateUserParams['field_address']['en'][0]['locality'] = $form_state['values']['city'];
$updateUserParams['field_phone'][LANGUAGE_NONE][0]['value'] = $form_state['values']['phone'];

user_save($user, $updateUserParams);

What am I doing wrong?

2 Answers 2

0

If you look user_save there special code at the top, that prevent updating password if pass not provided on $updateUserParams. So please check your other modules or Rules, that possibly resetting password each time, then user_save called.

0

@Nikit, your answer pushed me in the right direction, however, it wasn't correct.

the problem wasn't that I had the pass variable defined in my $updateUserParams, as you can see, I do not.

The problem was, that i had the pass variable defined in the $user I did my user_save with. I managed to fix the problem by unsetting the pass on my user object. And to be saved, I did the update on the $fulluser instead of the $user which is a global variable, and could potentially break things.

Here's the working code.

    global $user;
    global $language;
    $fullUser = user_load($user->uid);

    $updateUserParams = (array) $fullUser;

    $updateUserParams['language'] = $language->language;
    $updateUserParams['field_first_name'][LANGUAGE_NONE][0]['value'] = $form_state['values']['firstName'];
    $updateUserParams['field_surname'][LANGUAGE_NONE][0]['value'] = $form_state['values']['lastName'];

    $year = $form_state['values']['birthdate']['year'];
    $month = $form_state['values']['birthdate']['month'];
    $day = $form_state['values']['birthdate']['day'];

    $birthdate = date( "Y-m-d", strtotime($year.'-'.$month.'-'.$day));

    $updateUserParams['field_birthdate'][LANGUAGE_NONE][0]['value'] = $birthdate;
    $updateUserParams['gender'][LANGUAGE_NONE][0]['value'] = $form_state['values']['gender'];
    $updateUserParams['field_address']['en'][0]['thoroughfare'] = $form_state['values']['street'];
    $updateUserParams['field_address']['en'][0]['postal_code'] = $form_state['values']['postalCode'];
    $updateUserParams['field_address']['en'][0]['locality'] = $form_state['values']['city'];
    $updateUserParams['field_phone'][LANGUAGE_NONE][0]['value'] = $form_state['values']['phone'];

    unset($fullUser->pass);
    unset($updateUserParams['pass']);

    user_save($fullUser, $updateUserParams);

Your Answer

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

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