You are here

function gathercontent_import_process in GatherContent 8.5

Same name and namespace in other branches
  1. 8.3 gathercontent.module \gathercontent_import_process()
  2. 8.4 gathercontent.module \gathercontent_import_process()
  3. 7.3 gathercontent.import.inc \gathercontent_import_process()

Batch operation callback for importing items.

2 string references to 'gathercontent_import_process'
ContentImportSelectForm::submitForm in gathercontent_ui/src/Form/ContentImportSelectForm.php
Form submission handler.
drush_gathercontent_import in ./gathercontent.drush.inc
Implements drush_COMMAND().

File

./gathercontent.module, line 52
Main module file for GatherContent module.

Code

function gathercontent_import_process($gc_ids, array $import_options, MappingInterface $mapping, &$context = []) {
  $client = \Drupal::service('gathercontent.client');
  $migrationIds = $mapping
    ->getMigrations();
  if (!isset($context['results']['success'])) {
    $context['results']['success'] = 0;
  }
  if (!isset($context['results']['failed'])) {
    $context['results']['failed'] = 0;
  }
  if (!empty($migrationIds)) {
    foreach ($migrationIds as $migrationId) {

      /** @var \Drupal\migrate\Plugin\Migration $migration */
      $migration = \Drupal::service('plugin.manager.migration')
        ->createInstance($migrationId);
      if ($migration) {
        $messages = new MigrateMessageCapture();
        $executable = new MigrateExecutable($migration, $messages, [
          'idlist' => implode(',', $gc_ids),
          'import_options' => $import_options,
          'client' => $client,
        ]);
        $status = '';
        try {
          $status = $executable
            ->import();
          $context['results']['success'] += $executable
            ->getCreatedCount() + $executable
            ->getUpdatedCount();
          $context['results']['failed'] += $executable
            ->getFailedCount() + $executable
            ->getIgnoredCount();
          if ($executable
            ->getFailedCount() + $executable
            ->getIgnoredCount() > 0) {
            $executable
              ->rollback();
          }
        } catch (\Exception $e) {
          \Drupal::logger('gathercontent')
            ->error($e
            ->getMessage());
        }
        switch ($status) {
          case MigrationInterface::RESULT_FAILED:
            if ($migration
              ->getStatus() !== MigrationInterface::STATUS_IDLE) {
              $migration
                ->setStatus(MigrationInterface::STATUS_IDLE);
            }

            // Add and log any captured messages.
            foreach ($messages
              ->getMessages() as $message) {
              $context['results']['messages'][] = (string) $message;
              \Drupal::logger('gathercontent')
                ->error($message);
            }
            break;
        }
      }
    }
  }
}