1

On the Cancel account page, is it possible to disable the option "Delete the account and its content"?

Some roles have the permission to cancel user accounts, but they should not have the option to delete the user's content.

1
  • use hook_form_alter to remove the option. – No Sssweat Feb 15 '17 at 12:26
3

You can implement hook_form_alter() in a custom module or theme to either remove the radio button entirely, or disable it which grays it out. You'd need to add additional logic to check the role of the user and disable it appropriately. I've shown both examples in the code example below, just uncomment the one you want to use.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'user-multiple-cancel-confirm') {
    // Set '#access' to FALSE to remove the radio button.
    // $form['user_cancel_method']['user_cancel_delete']['#access'] = FALSE;

    // Set '#disabled' to TRUE to disable the radio button.
    // $form['user_cancel_method']['user_cancel_delete']['#disabled'] = TRUE;
  }
}
0

There are two routes to deleting a user in the admin UI. Vincent's example handles deleting a user from the People list. The following code also handles clicking the 'Cancel account' button on the user's profile edit form:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
        
  /* Debug... 
    file_put_contents(
      $_SERVER["DOCUMENT_ROOT"]."/sites/default/files/debug.log",
      "mymodule_form_alter()...form_id: ".$form_id."\nform: [\n".print_r($form, TRUE)."]\n\n-----------------------\n\n",
      FILE_APPEND
    );
  */
  
  switch ($form_id) {
        
    case 'user_cancel_confirm_form':
    case 'user_multiple_cancel_confirm':
    //
    // Only full Administrator accounts can delete user *and* their content...
    // Disable the cancel account options, apart from 'Delete the account and make its content belong to the Anonymous user.'
    //
    if (!user_access('administer modules'))  {
        //
        // Set '#disabled' to TRUE to disable the radio button.  (Still need to change the default value - see below)
        $form['user_cancel_method']['user_cancel_block']['#disabled'] = TRUE;
        $form['user_cancel_method']['user_cancel_block_unpublish']['#disabled'] = TRUE;
        $form['user_cancel_method']['user_cancel_delete']['#disabled'] = TRUE;
        
        // Set '#access' to FALSE to remove the radio button.  (Still need to change the default value - see below)
//        $form['user_cancel_method']['user_cancel_block']['#access'] = FALSE;
//        $form['user_cancel_method']['user_cancel_block_unpublish']['#access'] = FALSE;
//        $form['user_cancel_method']['user_cancel_delete']['#access'] = FALSE;
    }
    // For everyone, change the default radio button...
    $form['user_cancel_method']['user_cancel_block']['#default_value'] = 'user_cancel_reassign';
    $form['user_cancel_method']['user_cancel_block_unpublish']['#default_value'] = 'user_cancel_reassign';
    $form['user_cancel_method']['user_cancel_delete']['#default_value'] = 'user_cancel_reassign';
    $form['user_cancel_method']['user_cancel_reassign']['#default_value'] = 'user_cancel_reassign';
    break;

  }
}

Thanks again Vincent and hope this helps someone.

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.