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\Core\TempStore\PrivateTempStoreFactory;
10 use Drupal\user\UserStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a confirmation form for cancelling multiple user accounts.
18 class UserMultipleCancelConfirm extends ConfirmFormBase {
21 * The temp store factory.
23 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
25 protected $tempStoreFactory;
30 * @var \Drupal\user\UserStorageInterface
32 protected $userStorage;
37 * @var \Drupal\Core\Entity\EntityManagerInterface
39 protected $entityManager;
42 * Constructs a new UserMultipleCancelConfirm.
44 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
45 * The temp store factory.
46 * @param \Drupal\user\UserStorageInterface $user_storage
48 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
51 public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
52 $this->tempStoreFactory = $temp_store_factory;
53 $this->userStorage = $user_storage;
54 $this->entityManager = $entity_manager;
60 public static function create(ContainerInterface $container) {
62 $container->get('tempstore.private'),
63 $container->get('entity.manager')->getStorage('user'),
64 $container->get('entity.manager')
71 public function getFormId() {
72 return 'user_multiple_cancel_confirm';
78 public function getQuestion() {
79 return $this->t('Are you sure you want to cancel these user accounts?');
85 public function getCancelUrl() {
86 return new Url('entity.user.collection');
92 public function getConfirmText() {
93 return $this->t('Cancel accounts');
99 public function buildForm(array $form, FormStateInterface $form_state) {
100 // Retrieve the accounts to be canceled from the temp store.
101 /* @var \Drupal\user\Entity\User[] $accounts */
102 $accounts = $this->tempStoreFactory
103 ->get('user_user_operations_cancel')
104 ->get($this->currentUser()->id());
106 return $this->redirect('entity.user.collection');
111 $form['accounts'] = ['#tree' => TRUE];
112 foreach ($accounts as $account) {
113 $uid = $account->id();
114 $names[$uid] = $account->label();
115 // Prevent user 1 from being canceled.
117 $root = intval($uid) === 1 ? $account : $root;
120 $form['accounts'][$uid] = [
126 $form['account']['names'] = [
127 '#theme' => 'item_list',
131 // Output a notice that user 1 cannot be canceled.
133 $redirect = (count($accounts) == 1);
134 $message = $this->t('The user account %name cannot be canceled.', ['%name' => $root->label()]);
135 drupal_set_message($message, $redirect ? 'error' : 'warning');
136 // If only user 1 was selected, redirect to the overview.
138 return $this->redirect('entity.user.collection');
142 $form['operation'] = ['#type' => 'hidden', '#value' => 'cancel'];
144 $form['user_cancel_method'] = [
146 '#title' => $this->t('When cancelling these accounts'),
149 $form['user_cancel_method'] += user_cancel_methods();
151 // Allow to send the account cancellation confirmation mail.
152 $form['user_cancel_confirm'] = [
153 '#type' => 'checkbox',
154 '#title' => $this->t('Require email confirmation to cancel account'),
155 '#default_value' => FALSE,
156 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
158 // Also allow to send account canceled notification mail, if enabled.
159 $form['user_cancel_notify'] = [
160 '#type' => 'checkbox',
161 '#title' => $this->t('Notify user when account is canceled'),
162 '#default_value' => FALSE,
163 '#access' => $this->config('user.settings')->get('notify.status_canceled'),
164 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
167 $form = parent::buildForm($form, $form_state);
175 public function submitForm(array &$form, FormStateInterface $form_state) {
176 $current_user_id = $this->currentUser()->id();
178 // Clear out the accounts from the temp store.
179 $this->tempStoreFactory->get('user_user_operations_cancel')->delete($current_user_id);
180 if ($form_state->getValue('confirm')) {
181 foreach ($form_state->getValue('accounts') as $uid => $value) {
182 // Prevent programmatic form submissions from cancelling user 1.
186 // Prevent user administrators from deleting themselves without confirmation.
187 if ($uid == $current_user_id) {
188 $admin_form_mock = [];
189 $admin_form_state = $form_state;
190 $admin_form_state->unsetValue('user_cancel_confirm');
191 // The $user global is not a complete user entity, so load the full
193 $account = $this->userStorage->load($uid);
194 $admin_form = $this->entityManager->getFormObject('user', 'cancel');
195 $admin_form->setEntity($account);
196 // Calling this directly required to init form object with $account.
197 $admin_form->buildForm($admin_form_mock, $admin_form_state);
198 $admin_form->submitForm($admin_form_mock, $admin_form_state);
201 user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));
205 $form_state->setRedirect('entity.user.collection');