You are here

public static function WebformResultsExportController::batchProcess in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Controller/WebformResultsExportController.php \Drupal\webform\Controller\WebformResultsExportController::batchProcess()

Batch API callback; Write the header and rows of the export to the export file.

Parameters

\Drupal\webform\WebformInterface $webform: The webform.

\Drupal\Core\Entity\EntityInterface|null $source_entity: A webform source entity.

array $export_options: An associative array of export options.

mixed|array $context: The batch current context.

File

src/Controller/WebformResultsExportController.php, line 262

Class

WebformResultsExportController
Controller routines for webform submission export.

Namespace

Drupal\webform\Controller

Code

public static function batchProcess(WebformInterface $webform, EntityInterface $source_entity = NULL, array $export_options, &$context) {

  /** @var \Drupal\webform\WebformSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('webform_submission.exporter');
  $submission_exporter
    ->setWebform($webform);
  $submission_exporter
    ->setSourceEntity($source_entity);
  $submission_exporter
    ->setExporter($export_options);
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['offset'] = 0;
    $context['sandbox']['max'] = $submission_exporter
      ->getQuery()
      ->count()
      ->execute();

    // Store entity ids and not the actual webform or source entity in the
    // $context to prevent "The container was serialized" errors.
    // @see https://www.drupal.org/node/2822023
    $context['results']['webform_id'] = $webform
      ->id();
    $context['results']['source_entity_type'] = $source_entity ? $source_entity
      ->getEntityTypeId() : NULL;
    $context['results']['source_entity_id'] = $source_entity ? $source_entity
      ->id() : NULL;
    $context['results']['export_options'] = $export_options;
    $submission_exporter
      ->writeHeader();
  }

  // Write CSV records.
  $query = $submission_exporter
    ->getQuery();
  $query
    ->range($context['sandbox']['offset'], $submission_exporter
    ->getBatchLimit());
  $entity_ids = $query
    ->execute();
  $webform_submissions = WebformSubmission::loadMultiple($entity_ids);
  $submission_exporter
    ->writeRecords($webform_submissions);

  // Track progress.
  $context['sandbox']['progress'] += count($webform_submissions);
  $context['sandbox']['offset'] += $submission_exporter
    ->getBatchLimit();
  $context['message'] = t('Exported @count of @total submissions…', [
    '@count' => $context['sandbox']['progress'],
    '@total' => $context['sandbox']['max'],
  ]);

  // Track finished.
  if ($context['sandbox']['max'] > 0 && $context['sandbox']['progress'] !== $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    $context['finished'] = 1;
  }
}