You are here

private static function ContactStorageExportBatches::prepareMessages in Contact Storage Export 8

Prepare the contact_message objects for export to CSV.

Parameters

array $messages: The contact_message objects.

array $settings: The settings from the export form.

array $context: The batch context.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to ContactStorageExportBatches::prepareMessages()
ContactStorageExportBatches::getContactFormData in src/ContactStorageExportBatches.php
Get the submissions for the given contact form.

File

src/ContactStorageExportBatches.php, line 144

Class

ContactStorageExportBatches
Class ContactStorageExportBatches.

Namespace

Drupal\contact_storage_export

Code

private static function prepareMessages(array $messages, array $settings, array &$context) {

  /** @var \Drupal\contact_storage_export\ContactStorageExportService $exporter */
  $exporter = \Drupal::service('contact_storage_export.exporter');

  /** @var \Drupal\contact\MessageInterface $message */
  $message = reset($messages);

  // Prepare message labels.
  $settings['labels'] = $exporter
    ->getLabels($message);
  $csv_data = [];
  foreach ($messages as $contact_message) {
    $id = $contact_message
      ->id();

    // Serialize the contact message.
    $serialized_message = $exporter
      ->serialize($contact_message, $settings);

    // Add the row to our CSV data.
    $csv_data[] = $serialized_message;

    // Update the batch.
    $context['results']['current_id'] = $id;
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $id;

    // Set the current message.
    $context['message'] = t('Processed up to Contact Message ID @id. Your file will download immediately when complete.', [
      '@id' => $id,
    ]);
  }

  // Add the rows to our CSV data.
  $csv_string = $exporter
    ->encodeData($csv_data);
  self::writeToTempFile($csv_string);
}