1

I have written this hook in my custom module, just to track the status changes of the users.

/**
 * Implements @see hook_user_presave
 */
function mycustommodule_user_presave(&$edit, $account, $category) {  
$account_user_id_=$account->uid ;       
 $new_status=  $edit['status'];
 $previous_status= $account->status;
watchdog('attention', 
     'User  %ui changed status to: %newstat, previous status was %previousstat', 
     array('%ui' => $account_user_id_, '%newstat' => $new_status, '%previousstat' => $previous_status, WATCHDOG_NOTICE));
}

I noticed for example that when a user activates his newly created account by clicking on the link sent by email, the status moves to 1 which is good . But I also noticed other message that I couldn't know its cause:

"user 34 changed status to: , previous status was 1" . What makes the status of a user moves from 1 to null ?

1

You get NULL for $edit['status'] because the status hasn't been changed. The functions that calls user_save(), which invokes hook_user_presave() contain the following code.

// user_profile_form_submit()

$account_unchanged = clone $account;
entity_form_submit_build_entity('user', $account, $form, $form_state);

// Populate $edit with the properties of $account, which have been edited on
// this form by taking over all values, which appear in the form values too.
$edit = array_intersect_key((array) $account, $form_state['values']);
user_save($account_unchanged, $edit, $category);
// user_register_submit()

$account = $form['#user'];
entity_form_submit_build_entity('user', $account, $form, $form_state);

// Populate $edit with the properties of $account, which have been edited on
// this form by taking over all values, which appear in the form values too.
$edit = array_intersect_key((array) $account, $form_state['values']);
$account = user_save($account, $edit);

As the comment says, $edit contains only the account properties that have been changed, not every property. If you want to obtain the status value, you should use the following code.

$new_status = isset($edit['status']) ? $edit['status'] : $account->status;
4
  • Perfect answer I want now to tackle the case where the user activates his account by email ..I did the following and it worked if ($account->uid && $account->status === "0" && $edit['status']==1) {echo "hello";} . I want to ask you if my code "hello" will be executed when I have this case "user 34 changed status to: , previous status was 1" or not ? I can't test it for now but I assume it won't right? because $account->status === "0" is not true so we will exit the if condition before reaching the && $edit['status']==1 so i won't get an error saying undefined index status right? Apr 29 at 17:34
  • What I care for the most is that I don't want to get this error undefined index status which I used to get when a user edited his profile and obviously it was caused by his status that wasn't changed as you have said. Apr 29 at 17:36
  • 1
    Just verify the value has been set with isset($edit['status']) before accessing it. $new_status = $edit['status']; would cause the Undefined index error only when $edit['status'] doesn't exist, but $new_status = isset($edit['status']) ? $edit['status'] : $account->status; won't.
    – apaderno
    Apr 29 at 19:35
  • Thank you !!!!!! Apr 29 at 22:55

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.