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.
11 class UserCancelForm extends ContentEntityConfirmFormBase {
14 * Available account cancellation methods.
18 protected $cancelMethods;
21 * The user being cancelled.
23 * @var \Drupal\user\UserInterface
30 public function getQuestion() {
31 if ($this->entity->id() == $this->currentUser()->id()) {
32 return $this->t('Are you sure you want to cancel your account?');
34 return $this->t('Are you sure you want to cancel the account %name?', ['%name' => $this->entity->label()]);
40 public function getCancelUrl() {
41 return $this->entity->urlInfo();
47 public function getDescription() {
49 $default_method = $this->config('user.settings')->get('cancel_method');
50 if ($this->currentUser()->hasPermission('administer users') || $this->currentUser()->hasPermission('select account cancellation method')) {
51 $description = $this->t('Select the method to cancel the account above.');
53 // Options supplied via user_cancel_methods() can have a custom
54 // #confirm_description property for the confirmation form description.
55 elseif (isset($this->cancelMethods[$default_method]['#confirm_description'])) {
56 $description = $this->cancelMethods[$default_method]['#confirm_description'];
58 return $description . ' ' . $this->t('This action cannot be undone.');
64 public function getConfirmText() {
65 return $this->t('Cancel account');
71 public function buildForm(array $form, FormStateInterface $form_state) {
72 $user = $this->currentUser();
73 $this->cancelMethods = user_cancel_methods();
75 // Display account cancellation method selection, if allowed.
76 $admin_access = $user->hasPermission('administer users');
77 $form['user_cancel_method'] = [
79 '#title' => ($this->entity->id() == $user->id() ? $this->t('When cancelling your account') : $this->t('When cancelling the account')),
80 '#access' => $admin_access || $user->hasPermission('select account cancellation method'),
82 $form['user_cancel_method'] += $this->cancelMethods;
84 // Allow user administrators to skip the account cancellation confirmation
85 // mail (by default), as long as they do not attempt to cancel their own
87 $override_access = $admin_access && ($this->entity->id() != $user->id());
88 $form['user_cancel_confirm'] = [
89 '#type' => 'checkbox',
90 '#title' => $this->t('Require email confirmation to cancel account'),
91 '#default_value' => !$override_access,
92 '#access' => $override_access,
93 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
95 // Also allow to send account canceled notification mail, if enabled.
96 $default_notify = $this->config('user.settings')->get('notify.status_canceled');
97 $form['user_cancel_notify'] = [
98 '#type' => 'checkbox',
99 '#title' => $this->t('Notify user when account is canceled'),
100 '#default_value' => ($override_access ? FALSE : $default_notify),
101 '#access' => $override_access && $default_notify,
102 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
105 // Always provide entity id in the same form key as in the entity edit form.
106 $form['uid'] = ['#type' => 'value', '#value' => $this->entity->id()];
108 // Store the user permissions so that it can be altered in hook_form_alter()
112 '#value' => $user->hasPermission('administer users'),
115 $form = parent::buildForm($form, $form_state);
123 public function submitForm(array &$form, FormStateInterface $form_state) {
124 // Cancel account immediately, if the current user has administrative
125 // privileges, no confirmation mail shall be sent, and the user does not
126 // attempt to cancel the own account.
127 if (!$form_state->isValueEmpty('access') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity->id() != $this->currentUser()->id()) {
128 user_cancel($form_state->getValues(), $this->entity->id(), $form_state->getValue('user_cancel_method'));
130 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
133 // Store cancelling method and whether to notify the user in
135 // \Drupal\user\Controller\UserController::confirmCancel().
136 $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method');
137 $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify');
138 $this->entity->save();
139 _user_mail_notify('cancel_confirm', $this->entity);
140 drupal_set_message($this->t('A confirmation request to cancel your account has been sent to your email address.'));
141 $this->logger('user')->notice('Sent account cancellation request to %name %email.', ['%name' => $this->entity->label(), '%email' => '<' . $this->entity->getEmail() . '>']);
143 $form_state->setRedirect(
144 'entity.user.canonical',
145 ['user' => $this->entity->id()]