You are here

function user_delete_form_alter in User Delete 6.2

Same name and namespace in other branches
  1. 5 user_delete.module \user_delete_form_alter()
  2. 6 user_delete.module \user_delete_form_alter()

Implementation of hook_form_alter().

Perform several modifications to mimic the way Drupal 7 handle account cancellation. The modified forms are:

  • user_admin_account: for bulk operation on admin/user/user.
  • user_profile_form: to set cancel button when permission is set.
  • user_confirm_delete: to show the cancel methods when required.
  • user_multiple_delete_confirm: show cancel methods for bulk updates also.

File

./user_delete.module, line 83
Provide account cancellation methods and API to provide the same functionalty as Drupal 7 for cancelling accounts.

Code

function user_delete_form_alter(&$form, $form_state, $form_id) {
  global $user;
  if ($form_id == 'user_admin_account') {

    // Change title to show Cancel instead of delete
    $form['options']['operation']['#options']['delete'] = t('Cancel the selected accounts');
  }
  if ($form_id == 'user_profile_form') {

    // Replace 'Delete' button label with 'Cancel account'
    if (user_access('administer users') || user_access('cancel account') && $form['#uid'] == $user->uid) {
      $form['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Cancel account'),
        '#weight' => 31,
        '#submit' => array(
          'user_edit_delete_submit',
        ),
      );
    }

    /*
     * There are some reasons to keep this check commented. There are modules
     * that already protect this button to appear, and Drupal 6 has not an
     * special administrators group by default that can take control of the
     * site if uid 1 is removed.
     */

    //if ($user->uid == 1) {

    //  unset($form['delete']);

    //}
  }

  // Take control of the user multiple delete confirmation form
  if ($form_id == 'user_multiple_delete_confirm') {
    drupal_set_title(t('Are you sure you want to cancel these user accounts?'));
    $form['user_cancel_method'] = array(
      '#type' => 'item',
      '#title' => t('When cancelling these accounts'),
    );
    $form['user_cancel_method'] += user_delete_cancel_methods();

    // Remove method descriptions.
    foreach (element_children($form['user_cancel_method']) as $element) {
      unset($form['user_cancel_method'][$element]['#description']);
    }

    // Allow to send the account cancellation confirmation mail.
    $form['user_cancel_confirm'] = array(
      '#type' => 'checkbox',
      '#title' => t('Require e-mail confirmation to cancel account.'),
      '#default_value' => FALSE,
      '#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
    );

    // Also allow to send account canceled notification mail, if enabled.
    $form['user_cancel_notify'] = array(
      '#type' => 'checkbox',
      '#title' => t('Notify user when account is canceled.'),
      '#default_value' => FALSE,
      '#access' => variable_get('user_mail_status_canceled_notify', FALSE),
      '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
    );
    unset($form['description']);
    $form['description']['#value'] = $description;

    // Short control fields and set a new submit handler for this form.
    $form['actions']['#weight'] = 31;
    $form['actions']['submit']['#value'] = t('Cancel accounts');
    $form['#submit'] = array(
      'user_delete_multiple_confirm_submit',
    );
  }

  // Take control of the user delete confirmation form
  if ($form_id == 'user_confirm_delete') {
    $account = $form['_account']['#value'];

    // Display account cancellation method selection, if allowed.
    $default_method = variable_get('user_cancel_method', 'user_cancel_block');
    $admin_access = user_access('administer users');
    $can_select_method = $admin_access || user_access('select account cancellation method');
    $form['user_cancel_method'] = array(
      '#type' => 'item',
      '#title' => $account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account'),
      '#access' => $can_select_method,
    );
    $form['user_cancel_method'] += user_delete_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 = variable_get('user_mail_status_canceled_notify', FALSE);
    $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,
      ));
    }
    $description = '';
    if ($can_select_method) {
      $description = t('Select the method to cancel the account above.');
      foreach (element_children($form['user_cancel_method']) as $element) {
        unset($form['user_cancel_method'][$element]['#description']);
      }
    }
    else {

      // The radio button #description is used as description for the confirmation
      // form.
      foreach (element_children($form['user_cancel_method']) as $element) {
        if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
          $description = $form['user_cancel_method'][$element]['#description'];
        }
        unset($form['user_cancel_method'][$element]['#description']);
      }
    }

    // Always provide entity id in the same form key as in the entity edit form.
    $form['uid'] = array(
      '#type' => 'value',
      '#value' => $account->uid,
    );

    // Set new title and description
    drupal_set_title(t('Are you sure you want to cancel the account %name?', array(
      '%name' => $form['_account']['#value']->name,
    )));
    unset($form['description']);
    $form['description']['#value'] = $description;

    // Short control fields and set a new submit handler for this form.
    $form['actions']['#weight'] = 31;
    $form['actions']['submit']['#value'] = t('Cancel account');
    $form['#submit'] = array(
      'user_delete_confirm_form_submit',
    );
  }
}