You are here

public static function FieldEncryptUpdateForm::processBatch in Field Encryption 8.2

Processes batch updating of encryption fields.

Parameters

array $context: The batch API context.

File

src/Form/FieldEncryptUpdateForm.php, line 103

Class

FieldEncryptUpdateForm
Form builder for the field_encrypt field update page.

Namespace

Drupal\field_encrypt\Form

Code

public static function processBatch(&$context) {
  $queue_factory = \Drupal::service('queue');
  $queue_manager = \Drupal::service('plugin.manager.queue_worker');
  $config = \Drupal::config('field_encrypt.settings');

  /** @var QueueInterface $queue */
  $queue = $queue_factory
    ->get('cron_encrypted_field_update');
  $num_items = $queue
    ->numberOfItems();

  /** @var QueueWorkerInterface $queue_worker */
  $queue_worker = $queue_manager
    ->createInstance('cron_encrypted_field_update');
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = $num_items;
  }

  // Process entities in groups. Default batch size is 5.
  for ($i = 1; $i <= $config
    ->get('batch_size'); $i++) {
    if ($item = $queue
      ->claimItem()) {
      try {
        $queue_worker
          ->processItem($item->data);
        $queue
          ->deleteItem($item);
        $context['results']['items'][] = $item->data['entity_id'];
        $message = t('Updating @field_name on @entity_type with ID @entity_id', array(
          '@field_name' => $item->data['field_name'],
          '@entity_type' => $item->data['entity_type'],
          '@entity_id' => $item->data['entity_id'],
        ));
        $context['message'] = $message;
        $context['sandbox']['progress']++;
      } catch (SuspendQueueException $e) {
        $queue
          ->releaseItem($item);
      } catch (\Exception $e) {
        watchdog_exception('field_encrypt', $e);
        if (!isset($context['results']['errors'])) {
          $context['results']['errors'] = array();
        }
        $context['results']['errors'][] = $e
          ->getMessage();
      }
    }
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}