You are here

public static function MigrateBatchExecutable::batchProcessImport in Migrate source UI 8

Batch 'operation' callback.

Parameters

string $migration_id: The migration id.

array $options: The batch executable options.

array|\DrushBatchContext $context: The sandbox context.

File

src/MigrateBatchExecutable.php, line 84

Class

MigrateBatchExecutable
Defines a migrate executable class for batch migrations through UI.

Namespace

Drupal\migrate_source_ui

Code

public static function batchProcessImport($migration_id, array $options, &$context) {
  if (empty($context['sandbox'])) {
    $context['finished'] = 0;
    $context['sandbox'] = [];
    $context['sandbox']['total'] = 0;
    $context['sandbox']['counter'] = 0;
    $context['sandbox']['batch_limit'] = 0;
    $context['sandbox']['operation'] = MigrateBatchExecutable::BATCH_IMPORT;
  }

  // Prepare the migration executable.
  $message = new StubMigrationMessage();
  $definition = \Drupal::getContainer()
    ->get('plugin.manager.migration')
    ->getDefinition($migration_id);

  // Override the file path.
  $definition['source']['path'] = $options['file_path'];

  /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
  $migration = \Drupal::getContainer()
    ->get('plugin.manager.migration')
    ->createStubMigration($definition);
  $executable = new MigrateBatchExecutable($migration, $message, $options);
  if (empty($context['sandbox']['total'])) {
    $context['sandbox']['total'] = $executable
      ->getSource()
      ->count();
    $context['sandbox']['batch_limit'] = $executable
      ->calculateBatchLimit($context);
    $context['results'][$migration
      ->id()] = [
      '@numitems' => 0,
      '@created' => 0,
      '@updated' => 0,
      '@failures' => 0,
      '@ignored' => 0,
      '@name' => $migration
        ->id(),
    ];
  }

  // Every iteration, we reset out batch counter.
  $context['sandbox']['batch_counter'] = 0;

  // Make sure we know our batch context.
  $executable
    ->setBatchContext($context);

  // Do the import.
  $result = $executable
    ->import();

  // Store the result; will need to combine the results of all our iterations.
  $context['results'][$migration
    ->id()] = [
    '@numitems' => $context['results'][$migration
      ->id()]['@numitems'] + $executable
      ->getProcessedCount(),
    '@created' => $context['results'][$migration
      ->id()]['@created'] + $executable
      ->getCreatedCount(),
    '@updated' => $context['results'][$migration
      ->id()]['@updated'] + $executable
      ->getUpdatedCount(),
    '@failures' => $context['results'][$migration
      ->id()]['@failures'] + $executable
      ->getFailedCount(),
    '@ignored' => $context['results'][$migration
      ->id()]['@ignored'] + $executable
      ->getIgnoredCount(),
    '@name' => $migration
      ->id(),
  ];

  // Do some housekeeping.
  if ($result != MigrationInterface::RESULT_INCOMPLETE) {
    $context['finished'] = 1;
  }
  else {
    $context['sandbox']['counter'] = $context['results'][$migration
      ->id()]['@numitems'];
    if ($context['sandbox']['counter'] <= $context['sandbox']['total']) {
      $context['finished'] = (double) $context['sandbox']['counter'] / (double) $context['sandbox']['total'];
      $context['message'] = t('Importing %migration (@percent%).', [
        '%migration' => $migration
          ->label(),
        '@percent' => (int) ($context['finished'] * 100),
      ]);
    }
  }
}