You are here

function drush_gathercontent_import in GatherContent 8.5

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

Implements drush_COMMAND().

File

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

Code

function drush_gathercontent_import($mapping_id = NULL, $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) {
    drush_set_error('gathercontent_unknown_mapping_id', dt('Unknown mapping ID.'));
    return;
  }
  $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);
  $operations = [];
  foreach ($gc_ids as $gc_id) {
    $import_options[$gc_id] = new ImportOptions($publish, $createNewRevision, $status_id, $parent_menu_item);
  }
  $operations[] = [
    'gathercontent_import_process',
    [
      $gc_ids,
      $import_options,
      $mapping,
    ],
  ];
  $batch = [
    'title' => t('Importing'),
    'init_message' => t('Starting import'),
    'error_message' => t('An error occurred during processing'),
    'progress_message' => t('Processed @current out of @total.'),
    'progressive' => TRUE,
    'operations' => $operations,
    'finished' => 'gathercontent_drush_import_process_finished',
  ];
  batch_set($batch);
  drush_backend_batch_process();
}