3 namespace Drupal\user\Form;
5 use Drupal\Core\Entity\ContentEntityConfirmFormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Provides a confirmation form for cancelling user account.
13 class UserCancelForm extends ContentEntityConfirmFormBase {
16 * Available account cancellation methods.
20 protected $cancelMethods;
23 * The user being cancelled.
25 * @var \Drupal\user\UserInterface
32 public function getQuestion() {
33 if ($this->entity->id() == $this->currentUser()->id()) {
34 return $this->t('Are you sure you want to cancel your account?');
36 return $this->t('Are you sure you want to cancel the account %name?', ['%name' => $this->entity->label()]);
42 public function getCancelUrl() {
43 return $this->entity->urlInfo();
49 public function getDescription() {
51 $default_method = $this->config('user.settings')->get('cancel_method');
52 if ($this->currentUser()->hasPermission('administer users') || $this->currentUser()->hasPermission('select account cancellation method')) {
53 $description = $this->t('Select the method to cancel the account above.');
55 // Options supplied via user_cancel_methods() can have a custom
56 // #confirm_description property for the confirmation form description.
57 elseif (isset($this->cancelMethods[$default_method]['#confirm_description'])) {
58 $description = $this->cancelMethods[$default_method]['#confirm_description'];
60 return $description . ' ' . $this->t('This action cannot be undone.');
66 public function getConfirmText() {
67 return $this->t('Cancel account');
73 public function buildForm(array $form, FormStateInterface $form_state) {
74 $user = $this->currentUser();
75 $this->cancelMethods = user_cancel_methods();
77 // Display account cancellation method selection, if allowed.
78 $admin_access = $user->hasPermission('administer users');
79 $form['user_cancel_method'] = [
81 '#title' => ($this->entity->id() == $user->id() ? $this->t('When cancelling your account') : $this->t('When cancelling the account')),
82 '#access' => $admin_access || $user->hasPermission('select account cancellation method'),
84 $form['user_cancel_method'] += $this->cancelMethods;
86 // Allow user administrators to skip the account cancellation confirmation
87 // mail (by default), as long as they do not attempt to cancel their own
89 $override_access = $admin_access && ($this->entity->id() != $user->id());
90 $form['user_cancel_confirm'] = [
91 '#type' => 'checkbox',
92 '#title' => $this->t('Require email confirmation to cancel account'),
93 '#default_value' => !$override_access,
94 '#access' => $override_access,
95 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
97 // Also allow to send account canceled notification mail, if enabled.
98 $default_notify = $this->config('user.settings')->get('notify.status_canceled');
99 $form['user_cancel_notify'] = [
100 '#type' => 'checkbox',
101 '#title' => $this->t('Notify user when account is canceled'),
102 '#default_value' => ($override_access ? FALSE : $default_notify),
103 '#access' => $override_access && $default_notify,
104 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
107 // Always provide entity id in the same form key as in the entity edit form.
108 $form['uid'] = ['#type' => 'value', '#value' => $this->entity->id()];
110 // Store the user permissions so that it can be altered in hook_form_alter()
114 '#value' => $user->hasPermission('administer users'),
117 $form = parent::buildForm($form, $form_state);
125 public function submitForm(array &$form, FormStateInterface $form_state) {
126 // Cancel account immediately, if the current user has administrative
127 // privileges, no confirmation mail shall be sent, and the user does not
128 // attempt to cancel the own account.
129 if (!$form_state->isValueEmpty('access') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity->id() != $this->currentUser()->id()) {
130 user_cancel($form_state->getValues(), $this->entity->id(), $form_state->getValue('user_cancel_method'));
132 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
135 // Store cancelling method and whether to notify the user in
137 // \Drupal\user\Controller\UserController::confirmCancel().
138 $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method');
139 $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify');
140 $this->entity->save();
141 _user_mail_notify('cancel_confirm', $this->entity);
142 $this->messenger()->addStatus($this->t('A confirmation request to cancel your account has been sent to your email address.'));
143 $this->logger('user')->notice('Sent account cancellation request to %name %email.', ['%name' => $this->entity->label(), '%email' => '<' . $this->entity->getEmail() . '>']);
145 $form_state->setRedirect(
146 'entity.user.canonical',
147 ['user' => $this->entity->id()]