Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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  * @internal
12  */
13 class UserCancelForm extends ContentEntityConfirmFormBase {
14
15   /**
16    * Available account cancellation methods.
17    *
18    * @var array
19    */
20   protected $cancelMethods;
21
22   /**
23    * The user being cancelled.
24    *
25    * @var \Drupal\user\UserInterface
26    */
27   protected $entity;
28
29   /**
30    * {@inheritdoc}
31    */
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?');
35     }
36     return $this->t('Are you sure you want to cancel the account %name?', ['%name' => $this->entity->label()]);
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function getCancelUrl() {
43     return $this->entity->urlInfo();
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function getDescription() {
50     $description = '';
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.');
54     }
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'];
59     }
60     return $description . ' ' . $this->t('This action cannot be undone.');
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function getConfirmText() {
67     return $this->t('Cancel account');
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function buildForm(array $form, FormStateInterface $form_state) {
74     $user = $this->currentUser();
75     $this->cancelMethods = user_cancel_methods();
76
77     // Display account cancellation method selection, if allowed.
78     $admin_access = $user->hasPermission('administer users');
79     $form['user_cancel_method'] = [
80       '#type' => 'radios',
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'),
83     ];
84     $form['user_cancel_method'] += $this->cancelMethods;
85
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
88     // account.
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.'),
96     ];
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.'),
105     ];
106
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()];
109
110     // Store the user permissions so that it can be altered in hook_form_alter()
111     // if desired.
112     $form['access'] = [
113       '#type' => 'value',
114       '#value' => $user->hasPermission('administer users'),
115     ];
116
117     $form = parent::buildForm($form, $form_state);
118
119     return $form;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
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'));
131
132       $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
133     }
134     else {
135       // Store cancelling method and whether to notify the user in
136       // $this->entity for
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() . '>']);
144
145       $form_state->setRedirect(
146         'entity.user.canonical',
147         ['user' => $this->entity->id()]
148       );
149     }
150   }
151
152 }