Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/Form/UserMultipleCancelConfirm.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\Form; | |
4 | |
5 use Drupal\Core\Entity\EntityManagerInterface; | |
6 use Drupal\Core\Form\ConfirmFormBase; | |
7 use Drupal\Core\Form\FormStateInterface; | |
8 use Drupal\Core\Url; | |
9 use Drupal\user\PrivateTempStoreFactory; | |
10 use Drupal\user\UserStorageInterface; | |
11 use Symfony\Component\DependencyInjection\ContainerInterface; | |
12 | |
13 /** | |
14 * Provides a confirmation form for cancelling multiple user accounts. | |
15 */ | |
16 class UserMultipleCancelConfirm extends ConfirmFormBase { | |
17 | |
18 /** | |
19 * The temp store factory. | |
20 * | |
21 * @var \Drupal\user\PrivateTempStoreFactory | |
22 */ | |
23 protected $tempStoreFactory; | |
24 | |
25 /** | |
26 * The user storage. | |
27 * | |
28 * @var \Drupal\user\UserStorageInterface | |
29 */ | |
30 protected $userStorage; | |
31 | |
32 /** | |
33 * The entity manager. | |
34 * | |
35 * @var \Drupal\Core\Entity\EntityManagerInterface | |
36 */ | |
37 protected $entityManager; | |
38 | |
39 /** | |
40 * Constructs a new UserMultipleCancelConfirm. | |
41 * | |
42 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory | |
43 * The temp store factory. | |
44 * @param \Drupal\user\UserStorageInterface $user_storage | |
45 * The user storage. | |
46 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | |
47 * The entity manager. | |
48 */ | |
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; | |
53 } | |
54 | |
55 /** | |
56 * {@inheritdoc} | |
57 */ | |
58 public static function create(ContainerInterface $container) { | |
59 return new static( | |
60 $container->get('user.private_tempstore'), | |
61 $container->get('entity.manager')->getStorage('user'), | |
62 $container->get('entity.manager') | |
63 ); | |
64 } | |
65 | |
66 /** | |
67 * {@inheritdoc} | |
68 */ | |
69 public function getFormId() { | |
70 return 'user_multiple_cancel_confirm'; | |
71 } | |
72 | |
73 /** | |
74 * {@inheritdoc} | |
75 */ | |
76 public function getQuestion() { | |
77 return $this->t('Are you sure you want to cancel these user accounts?'); | |
78 } | |
79 | |
80 /** | |
81 * {@inheritdoc} | |
82 */ | |
83 public function getCancelUrl() { | |
84 return new Url('entity.user.collection'); | |
85 } | |
86 | |
87 /** | |
88 * {@inheritdoc} | |
89 */ | |
90 public function getConfirmText() { | |
91 return $this->t('Cancel accounts'); | |
92 } | |
93 | |
94 /** | |
95 * {@inheritdoc} | |
96 */ | |
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()); | |
103 if (!$accounts) { | |
104 return $this->redirect('entity.user.collection'); | |
105 } | |
106 | |
107 $root = NULL; | |
108 $names = []; | |
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. | |
114 if ($uid <= 1) { | |
115 $root = intval($uid) === 1 ? $account : $root; | |
116 continue; | |
117 } | |
118 $form['accounts'][$uid] = [ | |
119 '#type' => 'hidden', | |
120 '#value' => $uid, | |
121 ]; | |
122 } | |
123 | |
124 $form['account']['names'] = [ | |
125 '#theme' => 'item_list', | |
126 '#items' => $names, | |
127 ]; | |
128 | |
129 // Output a notice that user 1 cannot be canceled. | |
130 if (isset($root)) { | |
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. | |
135 if ($redirect) { | |
136 return $this->redirect('entity.user.collection'); | |
137 } | |
138 } | |
139 | |
140 $form['operation'] = ['#type' => 'hidden', '#value' => 'cancel']; | |
141 | |
142 $form['user_cancel_method'] = [ | |
143 '#type' => 'radios', | |
144 '#title' => $this->t('When cancelling these accounts'), | |
145 ]; | |
146 | |
147 $form['user_cancel_method'] += user_cancel_methods(); | |
148 | |
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.'), | |
155 ]; | |
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.'), | |
163 ]; | |
164 | |
165 $form = parent::buildForm($form, $form_state); | |
166 | |
167 return $form; | |
168 } | |
169 | |
170 /** | |
171 * {@inheritdoc} | |
172 */ | |
173 public function submitForm(array &$form, FormStateInterface $form_state) { | |
174 $current_user_id = $this->currentUser()->id(); | |
175 | |
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. | |
181 if ($uid <= 1) { | |
182 continue; | |
183 } | |
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 | |
190 // entity. | |
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); | |
197 } | |
198 else { | |
199 user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method')); | |
200 } | |
201 } | |
202 } | |
203 $form_state->setRedirect('entity.user.collection'); | |
204 } | |
205 | |
206 } |