annotate core/modules/user/src/Form/UserMultipleCancelConfirm.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\user\Form;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 6 use Drupal\Core\Form\ConfirmFormBase;
Chris@0 7 use Drupal\Core\Form\FormStateInterface;
Chris@0 8 use Drupal\Core\Url;
Chris@14 9 use Drupal\Core\TempStore\PrivateTempStoreFactory;
Chris@0 10 use Drupal\user\UserStorageInterface;
Chris@0 11 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Provides a confirmation form for cancelling multiple user accounts.
Chris@14 15 *
Chris@14 16 * @internal
Chris@0 17 */
Chris@0 18 class UserMultipleCancelConfirm extends ConfirmFormBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * The temp store factory.
Chris@0 22 *
Chris@14 23 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
Chris@0 24 */
Chris@0 25 protected $tempStoreFactory;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The user storage.
Chris@0 29 *
Chris@0 30 * @var \Drupal\user\UserStorageInterface
Chris@0 31 */
Chris@0 32 protected $userStorage;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * The entity manager.
Chris@0 36 *
Chris@0 37 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 38 */
Chris@0 39 protected $entityManager;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Constructs a new UserMultipleCancelConfirm.
Chris@0 43 *
Chris@14 44 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
Chris@0 45 * The temp store factory.
Chris@0 46 * @param \Drupal\user\UserStorageInterface $user_storage
Chris@0 47 * The user storage.
Chris@0 48 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 49 * The entity manager.
Chris@0 50 */
Chris@0 51 public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
Chris@0 52 $this->tempStoreFactory = $temp_store_factory;
Chris@0 53 $this->userStorage = $user_storage;
Chris@0 54 $this->entityManager = $entity_manager;
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * {@inheritdoc}
Chris@0 59 */
Chris@0 60 public static function create(ContainerInterface $container) {
Chris@0 61 return new static(
Chris@14 62 $container->get('tempstore.private'),
Chris@0 63 $container->get('entity.manager')->getStorage('user'),
Chris@0 64 $container->get('entity.manager')
Chris@0 65 );
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function getFormId() {
Chris@0 72 return 'user_multiple_cancel_confirm';
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 public function getQuestion() {
Chris@0 79 return $this->t('Are you sure you want to cancel these user accounts?');
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * {@inheritdoc}
Chris@0 84 */
Chris@0 85 public function getCancelUrl() {
Chris@0 86 return new Url('entity.user.collection');
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * {@inheritdoc}
Chris@0 91 */
Chris@0 92 public function getConfirmText() {
Chris@0 93 return $this->t('Cancel accounts');
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * {@inheritdoc}
Chris@0 98 */
Chris@0 99 public function buildForm(array $form, FormStateInterface $form_state) {
Chris@0 100 // Retrieve the accounts to be canceled from the temp store.
Chris@0 101 /* @var \Drupal\user\Entity\User[] $accounts */
Chris@0 102 $accounts = $this->tempStoreFactory
Chris@0 103 ->get('user_user_operations_cancel')
Chris@0 104 ->get($this->currentUser()->id());
Chris@0 105 if (!$accounts) {
Chris@0 106 return $this->redirect('entity.user.collection');
Chris@0 107 }
Chris@0 108
Chris@0 109 $root = NULL;
Chris@0 110 $names = [];
Chris@0 111 $form['accounts'] = ['#tree' => TRUE];
Chris@0 112 foreach ($accounts as $account) {
Chris@0 113 $uid = $account->id();
Chris@0 114 $names[$uid] = $account->label();
Chris@0 115 // Prevent user 1 from being canceled.
Chris@0 116 if ($uid <= 1) {
Chris@0 117 $root = intval($uid) === 1 ? $account : $root;
Chris@0 118 continue;
Chris@0 119 }
Chris@0 120 $form['accounts'][$uid] = [
Chris@0 121 '#type' => 'hidden',
Chris@0 122 '#value' => $uid,
Chris@0 123 ];
Chris@0 124 }
Chris@0 125
Chris@0 126 $form['account']['names'] = [
Chris@0 127 '#theme' => 'item_list',
Chris@0 128 '#items' => $names,
Chris@0 129 ];
Chris@0 130
Chris@0 131 // Output a notice that user 1 cannot be canceled.
Chris@0 132 if (isset($root)) {
Chris@0 133 $redirect = (count($accounts) == 1);
Chris@0 134 $message = $this->t('The user account %name cannot be canceled.', ['%name' => $root->label()]);
Chris@0 135 drupal_set_message($message, $redirect ? 'error' : 'warning');
Chris@0 136 // If only user 1 was selected, redirect to the overview.
Chris@0 137 if ($redirect) {
Chris@0 138 return $this->redirect('entity.user.collection');
Chris@0 139 }
Chris@0 140 }
Chris@0 141
Chris@0 142 $form['operation'] = ['#type' => 'hidden', '#value' => 'cancel'];
Chris@0 143
Chris@0 144 $form['user_cancel_method'] = [
Chris@0 145 '#type' => 'radios',
Chris@0 146 '#title' => $this->t('When cancelling these accounts'),
Chris@0 147 ];
Chris@0 148
Chris@0 149 $form['user_cancel_method'] += user_cancel_methods();
Chris@0 150
Chris@0 151 // Allow to send the account cancellation confirmation mail.
Chris@0 152 $form['user_cancel_confirm'] = [
Chris@0 153 '#type' => 'checkbox',
Chris@0 154 '#title' => $this->t('Require email confirmation to cancel account'),
Chris@0 155 '#default_value' => FALSE,
Chris@0 156 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
Chris@0 157 ];
Chris@0 158 // Also allow to send account canceled notification mail, if enabled.
Chris@0 159 $form['user_cancel_notify'] = [
Chris@0 160 '#type' => 'checkbox',
Chris@0 161 '#title' => $this->t('Notify user when account is canceled'),
Chris@0 162 '#default_value' => FALSE,
Chris@0 163 '#access' => $this->config('user.settings')->get('notify.status_canceled'),
Chris@0 164 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
Chris@0 165 ];
Chris@0 166
Chris@0 167 $form = parent::buildForm($form, $form_state);
Chris@0 168
Chris@0 169 return $form;
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * {@inheritdoc}
Chris@0 174 */
Chris@0 175 public function submitForm(array &$form, FormStateInterface $form_state) {
Chris@0 176 $current_user_id = $this->currentUser()->id();
Chris@0 177
Chris@0 178 // Clear out the accounts from the temp store.
Chris@0 179 $this->tempStoreFactory->get('user_user_operations_cancel')->delete($current_user_id);
Chris@0 180 if ($form_state->getValue('confirm')) {
Chris@0 181 foreach ($form_state->getValue('accounts') as $uid => $value) {
Chris@0 182 // Prevent programmatic form submissions from cancelling user 1.
Chris@0 183 if ($uid <= 1) {
Chris@0 184 continue;
Chris@0 185 }
Chris@0 186 // Prevent user administrators from deleting themselves without confirmation.
Chris@0 187 if ($uid == $current_user_id) {
Chris@0 188 $admin_form_mock = [];
Chris@0 189 $admin_form_state = $form_state;
Chris@0 190 $admin_form_state->unsetValue('user_cancel_confirm');
Chris@0 191 // The $user global is not a complete user entity, so load the full
Chris@0 192 // entity.
Chris@0 193 $account = $this->userStorage->load($uid);
Chris@0 194 $admin_form = $this->entityManager->getFormObject('user', 'cancel');
Chris@0 195 $admin_form->setEntity($account);
Chris@0 196 // Calling this directly required to init form object with $account.
Chris@0 197 $admin_form->buildForm($admin_form_mock, $admin_form_state);
Chris@0 198 $admin_form->submitForm($admin_form_mock, $admin_form_state);
Chris@0 199 }
Chris@0 200 else {
Chris@0 201 user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));
Chris@0 202 }
Chris@0 203 }
Chris@0 204 }
Chris@0 205 $form_state->setRedirect('entity.user.collection');
Chris@0 206 }
Chris@0 207
Chris@0 208 }