You are here

public function WebformSubmissionExporter::writeRecords in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionExporter.php \Drupal\webform\WebformSubmissionExporter::writeRecords()

Write webform results header to export file.

Parameters

\Drupal\webform\WebformSubmissionInterface[] $webform_submissions: A webform submission.

Overrides WebformSubmissionExporterInterface::writeRecords

1 call to WebformSubmissionExporter::writeRecords()
WebformSubmissionExporter::generate in src/WebformSubmissionExporter.php
Execute results exporter and write export to a temp file.

File

src/WebformSubmissionExporter.php, line 812

Class

WebformSubmissionExporter
Webform submission exporter.

Namespace

Drupal\webform

Code

public function writeRecords(array $webform_submissions) {
  $export_options = $this
    ->getExportOptions();
  $webform = $this
    ->getWebform();
  $is_archive = $this
    ->isArchive() && ($export_options['files'] || $export_options['attachments']);

  // Get files directories.
  $files_directories = [];
  if ($is_archive) {
    $stream_wrappers = array_keys($this->streamWrapperManager
      ->getNames(StreamWrapperInterface::WRITE_VISIBLE));
    foreach ($stream_wrappers as $stream_wrapper) {
      $files_directory = $this->fileSystem
        ->realpath($stream_wrapper . '://webform/' . $webform
        ->id());
      $files_directories[] = $files_directory;
    }
  }

  // Get attachment elements.
  $attachment_elements = $this
    ->getWebformExportAttachmentElements();
  $this
    ->getExporter()
    ->openExport();
  foreach ($webform_submissions as $webform_submission) {
    if ($is_archive) {
      $submission_base_name = $this
        ->getSubmissionBaseName($webform_submission);

      // Add managed file uploads to the archive.
      if ($export_options['files']) {
        foreach ($files_directories as $files_directory) {
          $submission_directory = $files_directory . '/' . $webform_submission
            ->id();
          if (file_exists($submission_directory) && $export_options['files']) {
            $this
              ->getExporter()
              ->addToArchive($submission_directory, $submission_base_name, [
              'remove_path' => $submission_directory,
            ]);
          }
        }
      }

      // Add attachment element files to the archive.
      if ($export_options['attachments']) {
        foreach ($attachment_elements as $attachment_element) {

          /** @var \Drupal\webform\Plugin\WebformElementAttachmentInterface $attachment_element_plugin */
          $attachment_element_plugin = $this->elementManager
            ->getElementInstance($attachment_element);
          $attachments = $attachment_element_plugin
            ->getExportAttachments($attachment_element, $webform_submission);
          foreach ($attachments as $attachment) {
            $this
              ->getExporter()
              ->addToArchive($attachment['filecontent'], $submission_base_name . '/attachments/' . $attachment['filename']);
          }
        }
      }
    }
    $this
      ->getExporter()
      ->writeSubmission($webform_submission);
  }
  $this
    ->getExporter()
    ->closeExport();
}