You are here

public function UserCancelForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/src/Form/UserCancelForm.php \Drupal\user\Form\UserCancelForm::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 ContentEntityConfirmFormBase::buildForm

File

core/modules/user/src/Form/UserCancelForm.php, line 85

Class

UserCancelForm
Provides a confirmation form for cancelling user account.

Namespace

Drupal\user\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $user = $this
    ->currentUser();
  $this->cancelMethods = user_cancel_methods();

  // Display account cancellation method selection, if allowed.
  $own_account = $this->entity
    ->id() == $user
    ->id();
  $this->selectCancel = $user
    ->hasPermission('administer users') || $user
    ->hasPermission('select account cancellation method');
  $form['user_cancel_method'] = [
    '#type' => 'radios',
    '#title' => $own_account ? $this
      ->t('When cancelling your account') : $this
      ->t('When cancelling the account'),
    '#access' => $this->selectCancel,
  ];
  $form['user_cancel_method'] += $this->cancelMethods;

  // When managing another user, can skip the account cancellation
  // confirmation mail (by default).
  $override_access = !$own_account;
  $form['user_cancel_confirm'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Require email confirmation to cancel account'),
    '#default_value' => !$override_access,
    '#access' => $override_access,
    '#description' => $this
      ->t('When enabled, the user must confirm the account cancellation via email.'),
  ];

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

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

  // Store the user permissions so that it can be altered in hook_form_alter()
  // if desired.
  $form['access'] = [
    '#type' => 'value',
    '#value' => !$own_account,
  ];
  $form = parent::buildForm($form, $form_state);
  return $form;
}