ProfileForm.php 1.68 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\user;

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 31 32 33

    return $element;
  }

  /**
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
    drupal_set_message($this->t('The changes have been saved.'));
42
  }
43

44 45 46
  /**
   * Provides a submit handler for the 'Cancel account' button.
   */
47
  public function editCancelSubmit($form, FormStateInterface $form_state) {
48
    $destination = [];
49 50
    $query = $this->getRequest()->query;
    if ($query->has('destination')) {
51
      $destination = ['destination' => $query->get('destination')];
52 53 54
      $query->remove('destination');
    }
    // We redirect from user/%/edit to user/%/cancel to make the tabs disappear.
55
    $form_state->setRedirect(
56
      'entity.user.cancel_form',
57 58
      ['user' => $this->entity->id()],
      ['query' => $destination]
59
    );
60 61
  }

62
}