You are here

public function UserCancelForm::buildForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 76
Contains \Drupal\user\Form\UserCancelForm.

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.
  $admin_access = $user
    ->hasPermission('administer users');
  $form['user_cancel_method'] = array(
    '#type' => 'radios',
    '#title' => $this->entity
      ->id() == $user
      ->id() ? $this
      ->t('When cancelling your account') : $this
      ->t('When cancelling the account'),
    '#access' => $admin_access || $user
      ->hasPermission('select account cancellation method'),
  );
  $form['user_cancel_method'] += $this->cancelMethods;

  // 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 && $this->entity
    ->id() != $user
    ->id();
  $form['user_cancel_confirm'] = array(
    '#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'] = array(
    '#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'] = array(
    '#type' => 'value',
    '#value' => $this->entity
      ->id(),
  );

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