As we know at the time of "Cancel user accounts", we have four options in drupal core that are following.
1. Disable the account and keep its content.
2. Disable the account and unpublish its content.
3. Delete the account and make its content belong to the Anonymous user.
4. Delete the account and its content.
In core user module (user.module) the function name "_user_cancel" has four cases in switchcase and one is "Default", the default is creating a problem. The issue is explained in below example.
Issue:
If some one want add more options(might be anything) apart from four like "Delete the account and make its content belong to the Super administrator." or any thing else. The "default:" case of switchcase will execute. It is stopper to add more options.
If we extend the code(hook_user_cancel) and will add one more option, it comes under "default" case in the switch case, as a result unwanted message appearing on the newly created option as the default option is being executed.
Proposed changes, I want to add a case before "user_cancel_delete".
case 'user_cancel_reassign_to_superadmin':
I had created a module(usercancel_contentassigntoadmin) to add one more option for user cancel.
Project (usercancel_contentassigntoadmin):
https://www.drupal.org/sandbox/jaipal/2408333 →
I am creating a patch to add a case in existing switchcase.
File name: user.module
Function name: _user_cancel
Modified Code in existing function:
function _user_cancel($edit, $account, $method) {
global $user;
switch ($method) {
case 'user_cancel_block':
case 'user_cancel_block_unpublish':
default:
// Send account blocked notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_blocked', $account);
}
user_save($account, array('status' => 0));
drupal_set_message(t('%name has been disabled.', array('%name' => $account->name)));
watchdog('user', 'Blocked user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
break;
case 'user_cancel_reassign':
case 'user_cancel_reassign_to_superadmin':
case 'user_cancel_delete':
// Send account canceled notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_canceled', $account);
}
user_delete($account->uid);
drupal_set_message(t('%name has been deleted.', array('%name' => $account->name)));
watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
break;
}
// After cancelling account, ensure that user is logged out. We can't destroy
// their session though, as we might have information in it, and we can't
// regenerate it because batch API uses the session ID, we will regenerate it
// in _user_cancel_session_regenerate().
if ($account->uid == $user->uid) {
$user = drupal_anonymous_user();
}
// Clear the cache for anonymous users.
cache_clear_all();
}