137868c4f51392e8f2a57e627dda30062a7ea0bf
[yaffs-website] / web / core / modules / user / src / ProfileForm.php
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Form handler for the profile forms.
9  *
10  * @internal
11  */
12 class ProfileForm extends AccountForm {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected function actions(array $form, FormStateInterface $form_state) {
18     $element = parent::actions($form, $form_state);
19
20     // The user account being edited.
21     $account = $this->entity;
22
23     // The user doing the editing.
24     $user = $this->currentUser();
25     $element['delete']['#type'] = 'submit';
26     $element['delete']['#value'] = $this->t('Cancel account');
27     $element['delete']['#submit'] = ['::editCancelSubmit'];
28     $element['delete']['#access'] = $account->id() > 1 && (($account->id() == $user->id() && $user->hasPermission('cancel account')) || $user->hasPermission('administer users'));
29
30     return $element;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function save(array $form, FormStateInterface $form_state) {
37     $account = $this->entity;
38     $account->save();
39     $form_state->setValue('uid', $account->id());
40
41     $this->messenger()->addStatus($this->t('The changes have been saved.'));
42   }
43
44   /**
45    * Provides a submit handler for the 'Cancel account' button.
46    */
47   public function editCancelSubmit($form, FormStateInterface $form_state) {
48     $destination = [];
49     $query = $this->getRequest()->query;
50     if ($query->has('destination')) {
51       $destination = ['destination' => $query->get('destination')];
52       $query->remove('destination');
53     }
54     // We redirect from user/%/edit to user/%/cancel to make the tabs disappear.
55     $form_state->setRedirect(
56       'entity.user.cancel_form',
57       ['user' => $this->entity->id()],
58       ['query' => $destination]
59     );
60   }
61
62 }