Form builder; confirm form for cancelling user account.
function user_cancel_confirm_form($form, &$form_state, $account) {
global $user;
$form['_account'] = array(
'#type' => 'value',
'#value' => $account,
);
// Display account cancellation method selection, if allowed.
$admin_access = user_access('administer users');
$can_select_method = $admin_access || user_access('select account cancellation method');
$form['user_cancel_method'] = array(
'#type' => 'radios',
'#title' => $account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account'),
'#access' => $can_select_method,
);
$form['user_cancel_method'] += user_cancel_methods();
// Allow user administrators to skip the account cancellation confirmation
// mail (by default), as long as they do not attempt to cancel their own
// account.
$override_access = $admin_access && $account->uid != $user->uid;
$form['user_cancel_confirm'] = array(
'#type' => 'checkbox',
'#title' => t('Require e-mail confirmation to cancel account.'),
'#default_value' => $override_access ? FALSE : TRUE,
'#access' => $override_access,
'#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
);
// Also allow to send account canceled notification mail, if enabled.
$default_notify = config('user.settings')
->get('notify.status_canceled');
$form['user_cancel_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify user when account is canceled.'),
'#default_value' => $override_access ? FALSE : $default_notify,
'#access' => $override_access && $default_notify,
'#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
);
// Prepare confirmation form page title and description.
if ($account->uid == $user->uid) {
$question = t('Are you sure you want to cancel your account?');
}
else {
$question = t('Are you sure you want to cancel the account %name?', array(
'%name' => $account->name,
));
}
$default_method = config('user.settings')
->get('cancel_method');
$description = NULL;
if ($can_select_method) {
$description = t('Select the method to cancel the account above.');
}
elseif (isset($form['user_cancel_method'][$default_method]['#confirm_description'])) {
$description = $form['user_cancel_method'][$default_method]['#confirm_description'];
}
// Always provide entity id in the same form key as in the entity edit form.
$form['uid'] = array(
'#type' => 'value',
'#value' => $account->uid,
);
return confirm_form($form, $question, 'user/' . $account->uid, $description . ' ' . t('This action cannot be undone.'), t('Cancel account'), t('Cancel'));
}