You are here

function drush_gathercontent_import in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 gathercontent.drush.inc \drush_gathercontent_import()
  2. 8.3 gathercontent.drush.inc \drush_gathercontent_import()
  3. 7.3 gathercontent.drush.inc \drush_gathercontent_import()

Implements drush_COMMAND().

File

./gathercontent.drush.inc, line 190
Drush command to cli config import.

Code

function drush_gathercontent_import($mapping_id = NULL, $node_update_method = NodeUpdateMethod::ALWAYS_UPDATE, $status_id = '', $parent_menu_item = NULL) {
  if ($mapping_id === NULL) {

    /** @var \Drupal\gathercontent\Entity\MappingInterface[] $gc_mappings */
    $gc_mappings = \Drupal::entityTypeManager()
      ->getStorage('gathercontent_mapping')
      ->loadMultiple();
    $options = [];
    foreach ($gc_mappings as $gc_mapping) {
      $options[$gc_mapping
        ->id()] = $gc_mapping
        ->id() . ' | ' . $gc_mapping
        ->getGathercontentProject() . ' | ' . $gc_mapping
        ->getGathercontentTemplate();
    }
    $mapping_id = drush_choice($options, dt('Select a mapping ID: '));
  }
  if (!$mapping_id) {
    return drush_set_error('gathercontent_unknown_mapping_id', dt('Unknown mapping ID.'));
  }
  $mapping = Mapping::load($mapping_id);
  $project_id = $mapping
    ->getGathercontentProjectId();
  $template_id = $mapping
    ->getGathercontentTemplateId();

  /** @var \Drupal\gathercontent\DrupalGatherContentClient $client */
  $client = \Drupal::service('gathercontent.client');

  /** @var \Cheppers\GatherContent\DataTypes\Item[] $items */
  $items = $client
    ->itemsGet($project_id);
  $publish = drush_get_option('publish', \Drupal::config('gathercontent.import')
    ->get('node_default_status'));
  $publish = $publish ? '1' : '0';
  $createNewRevision = drush_get_option('create-new-revision', \Drupal::config('gathercontent.import')
    ->get('node_create_new_revision'));
  $createNewRevision = $createNewRevision ? '1' : '0';
  $sql = drush_sql_get_class();
  if (!in_array('batch', $sql
    ->listTables())) {
    $bs = \Drupal::service('batch.storage');
    $db = \Drupal::database()
      ->schema();
    $db
      ->createTable('batch', $bs
      ->schemaDefinition());
  }

  // Create and start Batch processes.
  $isItemFromSelectedTemplate = function ($item) use ($template_id) {
    return $item->templateId === $template_id;
  };
  $itemToId = function ($item) {
    return $item->id;
  };
  $selected_items = array_filter($items, $isItemFromSelectedTemplate);
  $gc_ids = array_map($itemToId, $selected_items);
  $operation = Operation::create([
    'type' => 'import',
  ]);
  $operation
    ->save();
  $operations = [];
  foreach ($gc_ids as $gc_id) {
    $import_options = new ImportOptions($node_update_method, $publish, $createNewRevision, $status_id, $parent_menu_item, $operation
      ->uuid());
    $operations[] = [
      'gathercontent_import_process',
      [
        $gc_id,
        $import_options,
      ],
    ];
  }
  $batch = Importer::getBasicImportBatch();
  $batch['operations'] = $operations;
  $batch['finished'] = 'gathercontent_drush_import_process_finished';
  batch_set($batch);
  drush_backend_batch_process();

  // Display results.
  $mappings = [];
  $result = \Drupal::entityQuery('gathercontent_operation_item')
    ->condition('operation_uuid', $operation
    ->uuid())
    ->execute();
  if (!empty($result)) {
    $imported_items = OperationItem::loadMultiple($result);

    /** @var \Drupal\gathercontent\Entity\OperationItem $imported_item */
    foreach ($imported_items as $imported_item) {
      $mappings[$imported_item
        ->id()] = [
        'id' => $imported_item
          ->id(),
        'item_name' => $imported_item->item_name->value,
        'node_status' => $imported_item
          ->getItemStatus(),
        'import_status' => $imported_item
          ->getStatus(),
      ];
    }
  }
  return $mappings;
}