function UserMultipleCancelConfirm::buildForm

Same name in other branches
  1. 9 core/modules/user/src/Form/UserMultipleCancelConfirm.php \Drupal\user\Form\UserMultipleCancelConfirm::buildForm()
  2. 8.9.x core/modules/user/src/Form/UserMultipleCancelConfirm.php \Drupal\user\Form\UserMultipleCancelConfirm::buildForm()
  3. 10 core/modules/user/src/Form/UserMultipleCancelConfirm.php \Drupal\user\Form\UserMultipleCancelConfirm::buildForm()

Overrides ConfirmFormBase::buildForm

File

core/modules/user/src/Form/UserMultipleCancelConfirm.php, line 107

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.
    
    /** @var \Drupal\user\Entity\User[] $accounts */
    $accounts = $this->tempStoreFactory
        ->get('user_user_operations_cancel')
        ->get($this->currentUser()
        ->id());
    if (!$accounts) {
        return $this->redirect('entity.user.collection');
    }
    $root = NULL;
    $names = [];
    $form['accounts'] = [
        '#tree' => TRUE,
    ];
    foreach ($accounts as $account) {
        $uid = $account->id();
        $names[$uid] = $account->label();
        // Prevent user 1 from being canceled.
        if ($uid <= 1) {
            $root = intval($uid) === 1 ? $account : $root;
            continue;
        }
        $form['accounts'][$uid] = [
            '#type' => 'hidden',
            '#value' => $uid,
        ];
    }
    $form['account']['names'] = [
        '#theme' => 'item_list',
        '#items' => $names,
    ];
    // 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.', [
            '%name' => $root->label(),
        ]);
        $this->messenger()
            ->addMessage($message, $redirect ? MessengerInterface::TYPE_ERROR : MessengerInterface::TYPE_WARNING);
        // If only user 1 was selected, redirect to the overview.
        if ($redirect) {
            return $this->redirect('entity.user.collection');
        }
    }
    $form['operation'] = [
        '#type' => 'hidden',
        '#value' => 'cancel',
    ];
    // Display account cancellation method selection, if allowed.
    $user = $this->currentUser();
    $selectCancel = $user->hasPermission('administer users') || $user->hasPermission('select account cancellation method');
    $form['user_cancel_method'] = [
        '#type' => 'radios',
        '#title' => $this->t('Cancellation method'),
        '#access' => $selectCancel,
    ];
    $form['user_cancel_method'] += user_cancel_methods();
    if (!$selectCancel) {
        // Display an item to inform the user of the setting.
        $default_method = $form['user_cancel_method']['#default_value'];
        $form['user_cancel_method_show'] = [
            '#type' => 'item',
            '#title' => $this->t('When cancelling these accounts'),
            '#plain_text' => $form['user_cancel_method']['#options'][$default_method],
        ];
    }
    // Allow to send the account cancellation confirmation mail.
    $form['user_cancel_confirm'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Require email confirmation'),
        '#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'] = [
        '#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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.