3 namespace Drupal\user\Form;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\user\PrivateTempStoreFactory;
10 use Drupal\user\UserStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a confirmation form for cancelling multiple user accounts.
16 class UserMultipleCancelConfirm extends ConfirmFormBase {
19 * The temp store factory.
21 * @var \Drupal\user\PrivateTempStoreFactory
23 protected $tempStoreFactory;
28 * @var \Drupal\user\UserStorageInterface
30 protected $userStorage;
35 * @var \Drupal\Core\Entity\EntityManagerInterface
37 protected $entityManager;
40 * Constructs a new UserMultipleCancelConfirm.
42 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
43 * The temp store factory.
44 * @param \Drupal\user\UserStorageInterface $user_storage
46 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
49 public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
50 $this->tempStoreFactory = $temp_store_factory;
51 $this->userStorage = $user_storage;
52 $this->entityManager = $entity_manager;
58 public static function create(ContainerInterface $container) {
60 $container->get('user.private_tempstore'),
61 $container->get('entity.manager')->getStorage('user'),
62 $container->get('entity.manager')
69 public function getFormId() {
70 return 'user_multiple_cancel_confirm';
76 public function getQuestion() {
77 return $this->t('Are you sure you want to cancel these user accounts?');
83 public function getCancelUrl() {
84 return new Url('entity.user.collection');
90 public function getConfirmText() {
91 return $this->t('Cancel accounts');
97 public function buildForm(array $form, FormStateInterface $form_state) {
98 // Retrieve the accounts to be canceled from the temp store.
99 /* @var \Drupal\user\Entity\User[] $accounts */
100 $accounts = $this->tempStoreFactory
101 ->get('user_user_operations_cancel')
102 ->get($this->currentUser()->id());
104 return $this->redirect('entity.user.collection');
109 $form['accounts'] = ['#tree' => TRUE];
110 foreach ($accounts as $account) {
111 $uid = $account->id();
112 $names[$uid] = $account->label();
113 // Prevent user 1 from being canceled.
115 $root = intval($uid) === 1 ? $account : $root;
118 $form['accounts'][$uid] = [
124 $form['account']['names'] = [
125 '#theme' => 'item_list',
129 // Output a notice that user 1 cannot be canceled.
131 $redirect = (count($accounts) == 1);
132 $message = $this->t('The user account %name cannot be canceled.', ['%name' => $root->label()]);
133 drupal_set_message($message, $redirect ? 'error' : 'warning');
134 // If only user 1 was selected, redirect to the overview.
136 return $this->redirect('entity.user.collection');
140 $form['operation'] = ['#type' => 'hidden', '#value' => 'cancel'];
142 $form['user_cancel_method'] = [
144 '#title' => $this->t('When cancelling these accounts'),
147 $form['user_cancel_method'] += user_cancel_methods();
149 // Allow to send the account cancellation confirmation mail.
150 $form['user_cancel_confirm'] = [
151 '#type' => 'checkbox',
152 '#title' => $this->t('Require email confirmation to cancel account'),
153 '#default_value' => FALSE,
154 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
156 // Also allow to send account canceled notification mail, if enabled.
157 $form['user_cancel_notify'] = [
158 '#type' => 'checkbox',
159 '#title' => $this->t('Notify user when account is canceled'),
160 '#default_value' => FALSE,
161 '#access' => $this->config('user.settings')->get('notify.status_canceled'),
162 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
165 $form = parent::buildForm($form, $form_state);
173 public function submitForm(array &$form, FormStateInterface $form_state) {
174 $current_user_id = $this->currentUser()->id();
176 // Clear out the accounts from the temp store.
177 $this->tempStoreFactory->get('user_user_operations_cancel')->delete($current_user_id);
178 if ($form_state->getValue('confirm')) {
179 foreach ($form_state->getValue('accounts') as $uid => $value) {
180 // Prevent programmatic form submissions from cancelling user 1.
184 // Prevent user administrators from deleting themselves without confirmation.
185 if ($uid == $current_user_id) {
186 $admin_form_mock = [];
187 $admin_form_state = $form_state;
188 $admin_form_state->unsetValue('user_cancel_confirm');
189 // The $user global is not a complete user entity, so load the full
191 $account = $this->userStorage->load($uid);
192 $admin_form = $this->entityManager->getFormObject('user', 'cancel');
193 $admin_form->setEntity($account);
194 // Calling this directly required to init form object with $account.
195 $admin_form->buildForm($admin_form_mock, $admin_form_state);
196 $admin_form->submitForm($admin_form_mock, $admin_form_state);
199 user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));
203 $form_state->setRedirect('entity.user.collection');