View source  
  <?php
namespace Drupal\purge_users\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
class ConfirmationForm extends ConfirmFormBase {
  
  const NUMBER_OF_USERS_TO_SHOW = 50;
  
  public function getFormId() {
    return 'purge_users_confirm_form';
  }
  
  public function getQuestion() {
    return $this
      ->t('Purge user confirmation');
  }
  
  public function getCancelUrl() {
    return new Url('purge_users.settings');
  }
  
  public function getDescription() {
    return $this
      ->t('Are you sure you want to cancel these user accounts?');
  }
  
  public function getConfirmText() {
    return $this
      ->t('Confirm');
  }
  
  public function getCancelText() {
    return $this
      ->t('Cancel');
  }
  
  public function buildForm(array $form, FormStateInterface $form_state, $accounts = NULL) {
    $form = parent::buildForm($form, $form_state);
    $uids = purge_users_get_user_ids();
    $form['accounts'] = [
      '#prefix' => '<ul>',
      '#suffix' => '</ul>',
      '#tree' => TRUE,
    ];
    $uidsToShow = array_slice($uids, 0, self::NUMBER_OF_USERS_TO_SHOW);
    $accounts = User::loadMultiple($uidsToShow);
    foreach ($accounts as $account) {
      
      if ($account
        ->get('uid')->value <= 1) {
        continue;
      }
      $form['accounts']['uid' . $account
        ->get('uid')->value] = [
        '#type' => 'markup',
        '#value' => $account
          ->get('uid')->value,
        '#prefix' => '<li>',
        '#suffix' => $account
          ->get('name')->value . " <" . $account
          ->get('mail')->value . "> </li>\n",
      ];
    }
    if (count($uids) > self::NUMBER_OF_USERS_TO_SHOW) {
      $form['accounts']['and_more'] = [
        '#type' => 'markup',
        '#markup' => $this
          ->t('...and @more more.', [
          '@more' => count($uids) - self::NUMBER_OF_USERS_TO_SHOW,
        ]),
        '#prefix' => '<li>',
        '#suffix' => '</li>',
      ];
    }
    return parent::buildForm($form, $form_state);
  }
  
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $ids = purge_users_get_user_ids();
    
    $batch = [
      'operations' => [],
      'finished' => 'purge_users_batch_completed',
      'title' => $this
        ->t('Delete users'),
      'init_message' => $this
        ->t('Delete users operation is starting...'),
      'progress_message' => $this
        ->t('Processed @current out of @total.'),
      'error_message' => $this
        ->t('Delete users operation has encountered an error.'),
    ];
    
    foreach ($ids as $id) {
      $batch['operations'][] = [
        '\\Drupal\\purge_users\\Plugin\\BatchWorker\\BatchWorker::batchWorkerPurgeUsers',
        [
          $id,
        ],
      ];
    }
    
    batch_set($batch);
  }
}