You are here

public function ContentSyncCommands::import in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Commands/ContentSyncCommands.php \Drupal\content_sync\Commands\ContentSyncCommands::import()

Import content from a content directory.

@command content-sync:import @interact-config-label @option entity-types A list of entity type names separated by commas. @option uuids A list of UUIDs separated by commas. @option actions A list of Actions separated by commas. @option skiplist skip the change list before proceed with the import. @usage drush content-sync-import. @aliases csi,content-sync-import

Parameters

string|null $label: A content directory label (i.e. a key in \$content_directories array in settings.php).

array $options: The command options.

File

src/Commands/ContentSyncCommands.php, line 203

Class

ContentSyncCommands
A Drush commandfile.

Namespace

Drupal\content_sync\Commands

Code

public function import($label = NULL, array $options = [
  'entity-types' => '',
  'uuids' => '',
  'actions' => '',
  'skiplist' => FALSE,
]) {

  //Generate comparer with filters.
  $storage_comparer = new ContentStorageComparer($this->contentStorageSync, $this->contentStorage, $this->configManager);
  $change_list = [];
  $collections = $storage_comparer
    ->getAllCollectionNames();
  if (!empty($options['entity-types'])) {
    $entity_types = explode(',', $options['entity-types']);
    $match_collections = [];
    foreach ($entity_types as $entity_type) {
      $match_collections = $match_collections + preg_grep('/^' . $entity_type . '/', $collections);
    }
    $collections = $match_collections;
  }
  foreach ($collections as $collection) {
    if (!empty($options['uuids'])) {
      $storage_comparer
        ->createChangelistbyCollectionAndNames($collection, $options['uuids']);
    }
    else {
      $storage_comparer
        ->createChangelistbyCollection($collection);
    }
    if (!empty($options['actions'])) {
      $actions = explode(',', $options['actions']);
      foreach ($actions as $op) {
        if (in_array($op, [
          'create',
          'update',
          'delete',
        ])) {
          $change_list[$collection][$op] = $storage_comparer
            ->getChangelist($op, $collection);
        }
      }
    }
    else {
      $change_list[$collection] = $storage_comparer
        ->getChangelist(NULL, $collection);
    }
    $change_list = array_map('array_filter', $change_list);
    $change_list = array_filter($change_list);
  }
  unset($change_list['']);

  // Display the change list.
  if (empty($options['skiplist'])) {

    //Show differences
    $this
      ->output()
      ->writeln("Differences of the export directory to the active content:\n");

    // Print a table with changes in color.
    $table = self::contentChangesTable($change_list, $this
      ->output());
    $table
      ->render();

    // Ask to continue
    if (!$this
      ->io()
      ->confirm(dt('Do you want to import?'))) {
      throw new UserAbortException();
    }
  }

  //Process the Import Data
  $content_to_sync = [];
  $content_to_delete = [];
  foreach ($change_list as $collection => $actions) {
    if (!empty($actions['create'])) {
      $content_to_sync = array_merge($content_to_sync, $actions['create']);
    }
    if (!empty($actions['update'])) {
      $content_to_sync = array_merge($content_to_sync, $actions['update']);
    }
    if (!empty($actions['delete'])) {
      $content_to_delete = $actions['delete'];
    }
  }

  // Set the Import Batch
  if (!empty($content_to_sync) || !empty($content_to_delete)) {
    $batch = $this
      ->generateImportBatch($content_to_sync, $content_to_delete);
    batch_set($batch);
    drush_backend_batch_process();
  }
}