function UserCancelForm::submitForm

Same name and namespace in other branches
  1. 8.9.x core/modules/user/src/Form/UserCancelForm.php \Drupal\user\Form\UserCancelForm::submitForm()
  2. 10 core/modules/user/src/Form/UserCancelForm.php \Drupal\user\Form\UserCancelForm::submitForm()
  3. 11.x core/modules/user/src/Form/UserCancelForm.php \Drupal\user\Form\UserCancelForm::submitForm()

Overrides ContentEntityForm::submitForm

File

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

Class

UserCancelForm
Provides a confirmation form for cancelling user account.

Namespace

Drupal\user\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    // Cancel account immediately, if the current user has administrative
    // privileges, no confirmation mail shall be sent, and the user does not
    // attempt to cancel the own account.
    if (!$form_state->isValueEmpty('access') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity
        ->id() != $this->currentUser()
        ->id()) {
        user_cancel($form_state->getValues(), $this->entity
            ->id(), $form_state->getValue('user_cancel_method'));
        $form_state->setRedirectUrl($this->entity
            ->toUrl('collection'));
    }
    else {
        // Store cancelling method and whether to notify the user in
        // $this->entity for
        // \Drupal\user\Controller\UserController::confirmCancel().
        $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method');
        $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify');
        $this->entity
            ->save();
        _user_mail_notify('cancel_confirm', $this->entity);
        $this->messenger()
            ->addStatus($this->t('A confirmation request to cancel your account has been sent to your email address.'));
        $this->logger('user')
            ->notice('Sent account cancellation request to %name %email.', [
            '%name' => $this->entity
                ->label(),
            '%email' => '<' . $this->entity
                ->getEmail() . '>',
        ]);
        $form_state->setRedirect('entity.user.canonical', [
            'user' => $this->entity
                ->id(),
        ]);
    }
}

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