You are here

function bibcite_export_batch_all in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 modules/bibcite_export/bibcite_export.batch.inc \bibcite_export_batch_all()

Batch export callback. Export all entities by entity type.

Parameters

string $entity_type: Entity type identifier.

\Drupal\bibcite\Plugin\BibciteFormatInterface $format: Instance of format plugin.

array $context: The batch context array.

1 string reference to 'bibcite_export_batch_all'
ExportAllForm::submitForm in modules/bibcite_export/src/Form/ExportAllForm.php
Form submission handler.

File

modules/bibcite_export/bibcite_export.batch.inc, line 62
Batch callbacks.

Code

function bibcite_export_batch_all($entity_type, BibciteFormatInterface $format, array &$context) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type);
  $serializer = \Drupal::service('serializer');
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['max'] = $storage
      ->getQuery()
      ->count()
      ->execute();
  }
  if (empty($context['results'])) {

    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $context['results']['file'] = $file_system
      ->tempnam('temporary://', 'bibcite-');
    $context['results']['count'] = 0;
    $context['results']['format'] = $format;
    $context['results']['save_storage'] = TRUE;
  }
  $limit = 50;
  $result = $storage
    ->getQuery()
    ->range(0, $limit)
    ->condition('id', $context['sandbox']['current_id'], '>')
    ->sort('id')
    ->execute();
  $ids = array_keys($result);
  if (!$ids || !($entities = $storage
    ->loadMultiple($ids))) {
    $context['finished'] = 1;
    return;
  }
  $export_string = $serializer
    ->serialize($entities, $format
    ->getPluginId());
  file_put_contents($context['results']['file'], $export_string, FILE_APPEND);
  $latest_entity = end($entities);
  $context['sandbox']['progress'] += count($entities);
  $context['sandbox']['current_id'] = $latest_entity
    ->id();
  $context['results']['count'] = $context['sandbox']['progress'];
  $context['message'] = $latest_entity
    ->label();
  $progress = $context['sandbox']['progress'] / $context['sandbox']['max'];
  $context['finished'] = $progress < 1 ? $progress : 0.99;
}