You are here

public function UserMultipleCancelConfirm::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/src/Form/UserMultipleCancelConfirm.php \Drupal\user\Form\UserMultipleCancelConfirm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

core/modules/user/src/Form/UserMultipleCancelConfirm.php, line 198

Class

UserMultipleCancelConfirm
Provides a confirmation form for cancelling multiple user accounts.

Namespace

Drupal\user\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $current_user_id = $this
    ->currentUser()
    ->id();

  // Clear out the accounts from the temp store.
  $this->tempStoreFactory
    ->get('user_user_operations_cancel')
    ->delete($current_user_id);
  if ($form_state
    ->getValue('confirm')) {
    foreach ($form_state
      ->getValue('accounts') as $uid => $value) {

      // Prevent programmatic form submissions from cancelling user 1.
      if ($uid <= 1) {
        continue;
      }

      // Prevent user administrators from deleting themselves without confirmation.
      if ($uid == $current_user_id) {
        $admin_form_mock = [];
        $admin_form_state = $form_state;
        $admin_form_state
          ->unsetValue('user_cancel_confirm');

        // The $user global is not a complete user entity, so load the full
        // entity.
        $account = $this->userStorage
          ->load($uid);
        $admin_form = $this->entityTypeManager
          ->getFormObject('user', 'cancel');
        $admin_form
          ->setEntity($account);

        // Calling this directly required to init form object with $account.
        $admin_form
          ->buildForm($admin_form_mock, $admin_form_state);
        $admin_form
          ->submitForm($admin_form_mock, $admin_form_state);
      }
      else {
        user_cancel($form_state
          ->getValues(), $uid, $form_state
          ->getValue('user_cancel_method'));
      }
    }
  }
  $form_state
    ->setRedirect('entity.user.collection');
}