View source
<?php
namespace Drupal\user\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
class UserCancelForm extends ContentEntityConfirmFormBase {
protected $cancelMethods;
protected $selectCancel;
protected $entity;
public function getQuestion() {
if ($this->entity
->id() == $this
->currentUser()
->id()) {
return $this
->t('Are you sure you want to cancel your account?');
}
return $this
->t('Are you sure you want to cancel the account %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return $this->entity
->toUrl();
}
public function getDescription() {
$description = '';
$default_method = $this
->config('user.settings')
->get('cancel_method');
$own_account = $this->entity
->id() == $this
->currentUser()
->id();
if ($this->selectCancel) {
$description = $this
->t('Select the method to cancel the account above.');
}
elseif ($own_account && isset($this->cancelMethods[$default_method]['#confirm_description'])) {
$description = $this->cancelMethods[$default_method]['#confirm_description'];
}
else {
$description = $this->cancelMethods['#options'][$default_method];
}
return $description . ' ' . $this
->t('This action cannot be undone.');
}
public function getConfirmText() {
return $this
->t('Cancel account');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$user = $this
->currentUser();
$this->cancelMethods = user_cancel_methods();
$own_account = $this->entity
->id() == $user
->id();
$this->selectCancel = $user
->hasPermission('administer users') || $user
->hasPermission('select account cancellation method');
$form['user_cancel_method'] = [
'#type' => 'radios',
'#title' => $own_account ? $this
->t('When cancelling your account') : $this
->t('When cancelling the account'),
'#access' => $this->selectCancel,
];
$form['user_cancel_method'] += $this->cancelMethods;
$override_access = !$own_account;
$form['user_cancel_confirm'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Require email confirmation to cancel account'),
'#default_value' => !$override_access,
'#access' => $override_access,
'#description' => $this
->t('When enabled, the user must confirm the account cancellation via email.'),
];
$default_notify = $this
->config('user.settings')
->get('notify.status_canceled');
$form['user_cancel_notify'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Notify user when account is canceled'),
'#default_value' => $override_access ? FALSE : $default_notify,
'#access' => $override_access && $default_notify,
'#description' => $this
->t('When enabled, the user will receive an email notification after the account has been canceled.'),
];
$form['uid'] = [
'#type' => 'value',
'#value' => $this->entity
->id(),
];
$form['access'] = [
'#type' => 'value',
'#value' => !$own_account,
];
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->isValueEmpty('access') && $form_state
->isValueEmpty('user_cancel_confirm') && $this->entity
->id() != $this
->currentUser()
->id()) {
user_cancel($form_state
->getValues(), $this->entity
->id(), $form_state
->getValue('user_cancel_method'));
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
}
else {
$this->entity->user_cancel_method = $form_state
->getValue('user_cancel_method');
$this->entity->user_cancel_notify = $form_state
->getValue('user_cancel_notify');
$this->entity
->save();
_user_mail_notify('cancel_confirm', $this->entity);
$this
->messenger()
->addStatus($this
->t('A confirmation request to cancel your account has been sent to your email address.'));
$this
->logger('user')
->notice('Sent account cancellation request to %name %email.', [
'%name' => $this->entity
->label(),
'%email' => '<' . $this->entity
->getEmail() . '>',
]);
$form_state
->setRedirect('entity.user.canonical', [
'user' => $this->entity
->id(),
]);
}
}
}