0

I want to disable user using user_cancel method.

//A function that disables a user.
function myid_admin_disable_user(){
    try{    
        $edit = array(
            'user_cancel_notify' => true,
        );

        $method = 'user_cancel_block';  
        //5 is the user id of the account I want to disable.
        user_cancel($edit, 5, $method);     
        echo true;
    }catch(Exception $e){
        echo false;
    }
}

After running the function above, I can still use the account which is supposed to be disabled. Why? Whats wrong in my code above?

3

Per the docs in user_cancel():

Since the user cancellation process needs to be run in a batch, either Form API will invoke it, or batch_process() needs to be invoked after calling this function and should define the path to redirect to.

You need to invoke the batch API in order to trigger the result.

// Set the notify and cancel type options.    
user_cancel(array(
  'user_cancel_notify' => TRUE,
  'user_cancel_method' => 'user_cancel_delete',
), 5, 'user_cancel_delete');

// Invoke the Batch API, disable drupal_goto(), and execute.
$batch = &batch_get();
$batch['progressive'] = FALSE;
batch_process();
| improve this answer | |
  • Thank u but copying your last three lines of code to my code, does'nt do any changes. The 'batch' thing is new to me. I was currently reading about it. :) – user46175 Jun 17 '16 at 3:10
  • 1
    Check your $edit array, the form that invokes this function supplies more values than just the notify checkbox. – Shawn Conn Jun 17 '16 at 3:34
  • That is really the thing I'm confused of. I dont know what shall I put on my $edit variable. The function 'myid_admin_disable_user' is an invoked by ajax process. See my edited post above :) – user46175 Jun 17 '16 at 3:46
  • 1
    Updated, see user_multiple_cancel_confirm() and its submit handler for more detail how this function evokes it. – Shawn Conn Jun 17 '16 at 5:07
  • Thanks. Sorry for my wrong grammar in my comments. I could not edit it anymore. :) – user46175 Jun 17 '16 at 5:26
0

Since the method you want to use would block the user account, you can simply use the following code, to achieve what you want.

if ($account = user_load(5)) {
  _user_mail_notify('status_blocked', $account);
  user_save($account, array('status' => 0));
}

If you prefer not calling a private function, then you can use the following code.

if ($account = user_load(5)) {
  $notify = variable_get('user_mail_status_blocked_notify', TRUE);
  if ($notify) {
    $params['account'] = $account;
    drupal_mail('user', 'status_blocked', $account->mail, user_preferred_language($account), $params);
  }
  user_save($account, array('status' => 0));
}

user_cancel() is thought to be called in form submission handlers; plus, since you don't need to delete more accounts, you don't even need the batch API, for this task.

If you want to make the code even more complete, then you could change it as follows.

$edit = array('status' => 0);
if ($account = user_load(5)) {
  module_invoke_all('user_cancel', $edit, $account, 'user_cancel_block');
  $notify = variable_get('user_mail_status_blocked_notify', TRUE);
  if ($notify) {
    $params['account'] = $account;
    drupal_mail('user', 'status_blocked', $account->mail, user_preferred_language($account), $params);
  }
  user_save($account, $edit);
}

In this way, you also allows other modules to be notified a user account is being blocked.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy