Version 1
[yaffs-website] / web / core / modules / user / src / Form / UserCancelForm.php
1 <?php
2
3 namespace Drupal\user\Form;
4
5 use Drupal\Core\Entity\ContentEntityConfirmFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Provides a confirmation form for cancelling user account.
10  */
11 class UserCancelForm extends ContentEntityConfirmFormBase {
12
13   /**
14    * Available account cancellation methods.
15    *
16    * @var array
17    */
18   protected $cancelMethods;
19
20   /**
21    * The user being cancelled.
22    *
23    * @var \Drupal\user\UserInterface
24    */
25   protected $entity;
26
27   /**
28    * {@inheritdoc}
29    */
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?');
33     }
34     return $this->t('Are you sure you want to cancel the account %name?', ['%name' => $this->entity->label()]);
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function getCancelUrl() {
41     return $this->entity->urlInfo();
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getDescription() {
48     $description = '';
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.');
52     }
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'];
57     }
58     return $description . ' ' . $this->t('This action cannot be undone.');
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getConfirmText() {
65     return $this->t('Cancel account');
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function buildForm(array $form, FormStateInterface $form_state) {
72     $user = $this->currentUser();
73     $this->cancelMethods = user_cancel_methods();
74
75     // Display account cancellation method selection, if allowed.
76     $admin_access = $user->hasPermission('administer users');
77     $form['user_cancel_method'] = [
78       '#type' => 'radios',
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'),
81     ];
82     $form['user_cancel_method'] += $this->cancelMethods;
83
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
86     // account.
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.'),
94     ];
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.'),
103     ];
104
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()];
107
108     // Store the user permissions so that it can be altered in hook_form_alter()
109     // if desired.
110     $form['access'] = [
111       '#type' => 'value',
112       '#value' => $user->hasPermission('administer users'),
113     ];
114
115     $form = parent::buildForm($form, $form_state);
116
117     return $form;
118   }
119
120   /**
121    * {@inheritdoc}
122    */
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'));
129
130       $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
131     }
132     else {
133       // Store cancelling method and whether to notify the user in
134       // $this->entity for
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() . '>']);
142
143       $form_state->setRedirect(
144         'entity.user.canonical',
145         ['user' => $this->entity->id()]
146       );
147     }
148   }
149
150 }