You are here

public function ContentSynchronizerManager::launchImport in Content Synchronizer 3.x

Launch import from import id.

File

src/Service/ContentSynchronizerManager.php, line 255

Class

ContentSynchronizerManager
ContentSynchronizerManager service.

Namespace

Drupal\content_synchronizer\Service

Code

public function launchImport($importId, $publishType = ImportProcessor::DEFAULT_PUBLICATION_TYPE, $updateType = ImportProcessor::DEFAULT_UPDATE_TYPE) {

  // Check import id.
  $import = ImportEntity::load($importId);
  if (!$import) {
    throw new \Exception('No import entity found with id ' . $importId);
  }

  // Check publish type.
  $allowedCreateTypes = LaunchImportForm::getCreateOptions();
  if (!in_array($publishType, array_keys($allowedCreateTypes))) {
    throw new \Exception("Publish option must be in : " . implode('|', $allowedCreateTypes));
  }

  // Check update type.
  $allowedUpdateTypes = LaunchImportForm::getUpdateOptions();
  if (!in_array($updateType, array_keys($allowedUpdateTypes))) {
    throw new \Exception("Update option must be in : " . implode('|', $allowedUpdateTypes));
  }

  // Create import process.
  $importProcessor = new ImportProcessor($import);
  $importProcessor
    ->setCreationType($publishType);
  $importProcessor
    ->setUpdateType($updateType);

  // Loop into root entities.
  $rootEntities = $import
    ->getRootsEntities();
  $count = count($rootEntities);
  $importData = [
    'entities' => [],
  ];
  foreach ($rootEntities as $key => $rootEntityData) {
    try {
      $entity = $importProcessor
        ->importEntityFromRootData($rootEntityData);
      $status = array_key_exists('edit_url', $rootEntityData) ? $this
        ->t('Updated') : $this
        ->t('Created');
    } catch (\Exception $error) {
      $errorMessage = $error
        ->getMessage();
      $status = $this
        ->t('Error');
    }
    $importData['entities'][] = [
      '@key' => $key + 1,
      '@count' => $count,
      '@status' => $status,
      '@label' => $rootEntityData['label'],
      '@url' => $entity ? $entity
        ->toUrl()
        ->setAbsolute(TRUE)
        ->toString() : '',
      '@errorMessage' => $errorMessage ?: '',
    ];
  }

  // Close process.
  $import
    ->removeArchive();
  return $importData;
}