3 namespace Drupal\user\Form;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Messenger\MessengerInterface;
10 use Drupal\Core\TempStore\PrivateTempStoreFactory;
11 use Drupal\user\UserStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Provides a confirmation form for cancelling multiple user accounts.
19 class UserMultipleCancelConfirm extends ConfirmFormBase {
22 * The temp store factory.
24 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
26 protected $tempStoreFactory;
31 * @var \Drupal\user\UserStorageInterface
33 protected $userStorage;
38 * @var \Drupal\Core\Entity\EntityManagerInterface
40 protected $entityManager;
43 * Constructs a new UserMultipleCancelConfirm.
45 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
46 * The temp store factory.
47 * @param \Drupal\user\UserStorageInterface $user_storage
49 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
52 public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
53 $this->tempStoreFactory = $temp_store_factory;
54 $this->userStorage = $user_storage;
55 $this->entityManager = $entity_manager;
61 public static function create(ContainerInterface $container) {
63 $container->get('tempstore.private'),
64 $container->get('entity.manager')->getStorage('user'),
65 $container->get('entity.manager')
72 public function getFormId() {
73 return 'user_multiple_cancel_confirm';
79 public function getQuestion() {
80 return $this->t('Are you sure you want to cancel these user accounts?');
86 public function getCancelUrl() {
87 return new Url('entity.user.collection');
93 public function getConfirmText() {
94 return $this->t('Cancel accounts');
100 public function buildForm(array $form, FormStateInterface $form_state) {
101 // Retrieve the accounts to be canceled from the temp store.
102 /* @var \Drupal\user\Entity\User[] $accounts */
103 $accounts = $this->tempStoreFactory
104 ->get('user_user_operations_cancel')
105 ->get($this->currentUser()->id());
107 return $this->redirect('entity.user.collection');
112 $form['accounts'] = ['#tree' => TRUE];
113 foreach ($accounts as $account) {
114 $uid = $account->id();
115 $names[$uid] = $account->label();
116 // Prevent user 1 from being canceled.
118 $root = intval($uid) === 1 ? $account : $root;
121 $form['accounts'][$uid] = [
127 $form['account']['names'] = [
128 '#theme' => 'item_list',
132 // Output a notice that user 1 cannot be canceled.
134 $redirect = (count($accounts) == 1);
135 $message = $this->t('The user account %name cannot be canceled.', ['%name' => $root->label()]);
136 $this->messenger()->addMessage($message, $redirect ? MessengerInterface::TYPE_ERROR : MessengerInterface::TYPE_WARNING);
137 // If only user 1 was selected, redirect to the overview.
139 return $this->redirect('entity.user.collection');
143 $form['operation'] = ['#type' => 'hidden', '#value' => 'cancel'];
145 $form['user_cancel_method'] = [
147 '#title' => $this->t('When cancelling these accounts'),
150 $form['user_cancel_method'] += user_cancel_methods();
152 // Allow to send the account cancellation confirmation mail.
153 $form['user_cancel_confirm'] = [
154 '#type' => 'checkbox',
155 '#title' => $this->t('Require email confirmation to cancel account'),
156 '#default_value' => FALSE,
157 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
159 // Also allow to send account canceled notification mail, if enabled.
160 $form['user_cancel_notify'] = [
161 '#type' => 'checkbox',
162 '#title' => $this->t('Notify user when account is canceled'),
163 '#default_value' => FALSE,
164 '#access' => $this->config('user.settings')->get('notify.status_canceled'),
165 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
168 $form = parent::buildForm($form, $form_state);
176 public function submitForm(array &$form, FormStateInterface $form_state) {
177 $current_user_id = $this->currentUser()->id();
179 // Clear out the accounts from the temp store.
180 $this->tempStoreFactory->get('user_user_operations_cancel')->delete($current_user_id);
181 if ($form_state->getValue('confirm')) {
182 foreach ($form_state->getValue('accounts') as $uid => $value) {
183 // Prevent programmatic form submissions from cancelling user 1.
187 // Prevent user administrators from deleting themselves without confirmation.
188 if ($uid == $current_user_id) {
189 $admin_form_mock = [];
190 $admin_form_state = $form_state;
191 $admin_form_state->unsetValue('user_cancel_confirm');
192 // The $user global is not a complete user entity, so load the full
194 $account = $this->userStorage->load($uid);
195 $admin_form = $this->entityManager->getFormObject('user', 'cancel');
196 $admin_form->setEntity($account);
197 // Calling this directly required to init form object with $account.
198 $admin_form->buildForm($admin_form_mock, $admin_form_state);
199 $admin_form->submitForm($admin_form_mock, $admin_form_state);
202 user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));
206 $form_state->setRedirect('entity.user.collection');