View source
<?php
namespace Drupal\user\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserMultipleCancelConfirm extends ConfirmFormBase {
protected $tempStoreFactory;
protected $userStorage;
protected $entityTypeManager;
public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityTypeManagerInterface $entity_type_manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->userStorage = $user_storage;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'user_multiple_cancel_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to cancel these user accounts?');
}
public function getCancelUrl() {
return new Url('entity.user.collection');
}
public function getConfirmText() {
return $this
->t('Cancel accounts');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$accounts = $this->tempStoreFactory
->get('user_user_operations_cancel')
->get($this
->currentUser()
->id());
if (!$accounts) {
return $this
->redirect('entity.user.collection');
}
$root = NULL;
$names = [];
$form['accounts'] = [
'#tree' => TRUE,
];
foreach ($accounts as $account) {
$uid = $account
->id();
$names[$uid] = $account
->label();
if ($uid <= 1) {
$root = intval($uid) === 1 ? $account : $root;
continue;
}
$form['accounts'][$uid] = [
'#type' => 'hidden',
'#value' => $uid,
];
}
$form['account']['names'] = [
'#theme' => 'item_list',
'#items' => $names,
];
if (isset($root)) {
$redirect = count($accounts) == 1;
$message = $this
->t('The user account %name cannot be canceled.', [
'%name' => $root
->label(),
]);
$this
->messenger()
->addMessage($message, $redirect ? MessengerInterface::TYPE_ERROR : MessengerInterface::TYPE_WARNING);
if ($redirect) {
return $this
->redirect('entity.user.collection');
}
}
$form['operation'] = [
'#type' => 'hidden',
'#value' => 'cancel',
];
$user = $this
->currentUser();
$selectCancel = $user
->hasPermission('administer users') || $user
->hasPermission('select account cancellation method');
$form['user_cancel_method'] = [
'#type' => 'radios',
'#title' => $this
->t('When cancelling these accounts'),
'#access' => $selectCancel,
];
$form['user_cancel_method'] += user_cancel_methods();
if (!$selectCancel) {
$default_method = $form['user_cancel_method']['#default_value'];
$form['user_cancel_method_show'] = [
'#type' => 'item',
'#title' => $this
->t('When cancelling these accounts'),
'#plain_text' => $form['user_cancel_method']['#options'][$default_method],
];
}
$form['user_cancel_confirm'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Require email confirmation to cancel account'),
'#default_value' => FALSE,
'#description' => $this
->t('When enabled, the user must confirm the account cancellation via email.'),
];
$form['user_cancel_notify'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Notify user when account is canceled'),
'#default_value' => FALSE,
'#access' => $this
->config('user.settings')
->get('notify.status_canceled'),
'#description' => $this
->t('When enabled, the user will receive an email notification after the account has been canceled.'),
];
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_user_id = $this
->currentUser()
->id();
$this->tempStoreFactory
->get('user_user_operations_cancel')
->delete($current_user_id);
if ($form_state
->getValue('confirm')) {
foreach ($form_state
->getValue('accounts') as $uid => $value) {
if ($uid <= 1) {
continue;
}
if ($uid == $current_user_id) {
$admin_form_mock = [];
$admin_form_state = $form_state;
$admin_form_state
->unsetValue('user_cancel_confirm');
$account = $this->userStorage
->load($uid);
$admin_form = $this->entityTypeManager
->getFormObject('user', 'cancel');
$admin_form
->setEntity($account);
$admin_form
->buildForm($admin_form_mock, $admin_form_state);
$admin_form
->submitForm($admin_form_mock, $admin_form_state);
}
else {
user_cancel($form_state
->getValues(), $uid, $form_state
->getValue('user_cancel_method'));
}
}
}
$form_state
->setRedirect('entity.user.collection');
}
}