You are here

public function ContentSyncCommands::export 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::export()

Export Drupal content to a directory.

@command content-sync:export @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 files A value none/base64/folder - default folder. @option include-dependencies export content dependencies. @option skiplist skip the change list before proceed with the export. @usage drush content-sync-export. @aliases cse,content-sync-export.

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 299

Class

ContentSyncCommands
A Drush commandfile.

Namespace

Drupal\content_sync\Commands

Code

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

  //Generate comparer with filters.
  $storage_comparer = new ContentStorageComparer($this->contentStorage, $this->contentStorageSync, $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 active content to the export directory:\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 export?'))) {
      throw new UserAbortException();
    }
  }

  //Process the Export.
  $entities_list = [];
  foreach ($change_list as $collection => $changes) {

    //$storage_comparer->getTargetStorage($collection)->deleteAll();
    foreach ($changes as $change => $contents) {
      switch ($change) {
        case 'delete':
          foreach ($contents as $content) {
            $storage_comparer
              ->getTargetStorage($collection)
              ->delete($content);
          }
          break;
        case 'update':
        case 'create':
          foreach ($contents as $content) {

            //$data = $storage_comparer->getSourceStorage($collection)->read($content);

            //$storage_comparer->getTargetStorage($collection)->write($content, $data);
            $entity = explode('.', $content);
            $entities_list[] = [
              'entity_type' => $entity[0],
              'entity_uuid' => $entity[2],
            ];
          }
          break;
      }
    }
  }

  // Files options
  $include_files = self::processFilesOption($options);

  // Set the Export Batch
  if (!empty($entities_list)) {
    $batch = $this
      ->generateExportBatch($entities_list, [
      'export_type' => 'folder',
      'include_files' => $include_files,
      'include_dependencies' => $options['include-dependencies'],
    ]);
    batch_set($batch);
    drush_backend_batch_process();
  }
}