0

Currently in Drupal 7 I have two conditions in hook_user_presave,
1. Is account is being activated
2. Is account new


function hook_user_presave(&$edit, $account, $category) {
    if (isset($account->is_new) && !empty($account->is_new)) {
        //If account is new then execute my code here
    }

    if ((isset($account->uid)) && (isset($account->status)) && ($account->status == 0) && ($edit['status'] == 1)) {
        //If account is being activated for the first time then execute my code here
    }
}

I want to convert this code to Drupal 8, What should be the conditions?


function hook_user_presave($account) {

    //Condition 1

    //Condition 2
}

Is there any other way to achieve this?
Any help would be appreciated. Thanks.

1 Answer 1

5

To check if the user is new:

$account->isNew()

To check if the user is active:

$account->isActive()

See User API isNew or User API isActive for more information.

1
  • It's working. I added getLastAccessedTime condition to make sure that account is being activated for the first time. Thanks a lot for links, Very helpful. Sep 29, 2017 at 16:52

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.