You are here

public function UserMultipleCancelConfirm::buildForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Form/UserMultipleCancelConfirm.php \Drupal\user\Form\UserMultipleCancelConfirm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

core/modules/user/src/Form/UserMultipleCancelConfirm.php, line 102
Contains \Drupal\user\Form\UserMultipleCancelConfirm.

Class

UserMultipleCancelConfirm
Provides a confirmation form for cancelling multiple user accounts.

Namespace

Drupal\user\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Retrieve the accounts to be canceled from the temp store.
  $accounts = $this->tempStoreFactory
    ->get('user_user_operations_cancel')
    ->get($this
    ->currentUser()
    ->id());
  if (!$accounts) {
    return $this
      ->redirect('entity.user.collection');
  }
  $root = NULL;
  $form['accounts'] = array(
    '#prefix' => '<ul>',
    '#suffix' => '</ul>',
    '#tree' => TRUE,
  );
  foreach ($accounts as $account) {
    $uid = $account
      ->id();

    // Prevent user 1 from being canceled.
    if ($uid <= 1) {
      $root = intval($uid) === 1 ? $account : $root;
      continue;
    }
    $form['accounts'][$uid] = array(
      '#type' => 'hidden',
      '#value' => $uid,
      '#prefix' => '<li>',
      '#suffix' => $account
        ->label() . "</li>\n",
    );
  }

  // Output a notice that user 1 cannot be canceled.
  if (isset($root)) {
    $redirect = count($accounts) == 1;
    $message = $this
      ->t('The user account %name cannot be canceled.', array(
      '%name' => $root
        ->label(),
    ));
    drupal_set_message($message, $redirect ? 'error' : 'warning');

    // If only user 1 was selected, redirect to the overview.
    if ($redirect) {
      return $this
        ->redirect('entity.user.collection');
    }
  }
  $form['operation'] = array(
    '#type' => 'hidden',
    '#value' => 'cancel',
  );
  $form['user_cancel_method'] = array(
    '#type' => 'radios',
    '#title' => $this
      ->t('When cancelling these accounts'),
  );
  $form['user_cancel_method'] += user_cancel_methods();

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

  // Also allow to send account canceled notification mail, if enabled.
  $form['user_cancel_notify'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Notify user when account is canceled'),
    '#default_value' => FALSE,
    '#access' => $this
      ->config('user.settings')
      ->get('notify.status_canceled'),
    '#description' => $this
      ->t('When enabled, the user will receive an email notification after the account has been canceled.'),
  );
  $form = parent::buildForm($form, $form_state);
  return $form;
}