ProfileForm.php 1.7 KB

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