You are here

public static function EntityExportCsvBatch::prepareExportFile in Entity Export CSV 8

Prepare the export file.

Parameters

string $entity_type_id: The entity type on which to export.

string $bundle: The entity bundle type.

array $context: An array of the batch context.

Return value

false|string The file path or false

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to EntityExportCsvBatch::prepareExportFile()
EntityExportCsvBatch::export in src/EntityExportCsvBatch.php
Export entity data.

File

src/EntityExportCsvBatch.php, line 292

Class

EntityExportCsvBatch
Define entity export csv batch.

Namespace

Drupal\entity_export_csv

Code

public static function prepareExportFile($entity_type_id, $bundle, array &$context) {

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $private_system_file = PrivateStream::basePath();
  if (!$private_system_file) {
    $directory = EntityExportCsvManagerInterface::TEMPORARY_DIRECTORY;
  }
  else {
    $directory = EntityExportCsvManagerInterface::PRIVATE_DIRECTORY;
  }
  $file_system
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
  $time = time();
  $filename = $entity_type_id . '_' . $bundle . '_' . $time . '.csv';
  $destination = $directory . '/' . $filename;
  $file = file_save_data('', $destination, FileSystemInterface::EXISTS_REPLACE);
  $file
    ->setTemporary();
  $file
    ->save();
  $file_path = $file_system
    ->realpath($destination);
  $file_uri = $file
    ->getFileUri();
  $context['results']['filename'] = $filename;
  $context['results']['file_path'] = $file_path;
  $context['results']['file_uri'] = $file_uri;
  return $file_path;
}