You are here

function drush_content_export_yaml_cex_entity in Content Export YAML 8

File

drush/content_export_yaml.drush.inc, line 148

Code

function drush_content_export_yaml_cex_entity($entity = NULL, $bundle = NULL, $id = NULL) {
  if ($entity == NULL) {
    $entity_list_name = array_keys(\Drupal::entityTypeManager()
      ->getDefinitions());
    drush_print('Entity Type is required , Following avalaible : ' . implode(" - ", $entity_list_name));
    return NULL;
  }

  // $bundle_list = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();
  if ($bundle == NULL) {
    $bundle_list_name = \Drupal::service('entity_type.bundle.info')
      ->getBundleInfo($entity);
    drush_print('Bundle is required ,it should be one of : ' . implode(" - ", array_keys($bundle_list_name)));
    return NULL;
  }
  $export = new ContentExport();
  if (!empty($bundle_list_name) && !in_array($bundle, array_keys($bundle_list_name))) {
    drush_print('That ' . $bundle . ' bundle  not exist , it should be one of : ' . implode(" - ", array_keys($bundle_list_name)));
    return NULL;
  }
  if ($id) {
    $start = $id;
    $end = $id;
  }
  else {
    drush_print('argument add [ID] or all is required for ' . $bundle . ' bundle');
    return NULL;
  }
  if ($id == 'all') {
    $result = $export
      ->load_entity_list($entity, $bundle);
  }
  else {
    $result = $export
      ->load_entity_list($entity, $bundle, [
      $start,
      $end,
    ]);
  }
  if (!empty($result)) {
    if (drush_confirm(dt('Find items ' . sizeof($result) . ' , Are you sure to continue the process ?'))) {
      $total = sizeof($result);
      foreach ($result as $key => $item) {
        $info = [
          'id' => $item,
          'entity' => $entity,
          'index' => $key,
          'total' => $total,
        ];
        $operations[] = [
          'content_export_yaml_export_entity_batch_process',
          [
            $info,
          ],
        ];
      }
      $batch = [
        'operations' => $operations,
        'title' => t('Export node content process'),
        'init_message' => t('Starting...'),
        'progress_message' => t('Completed @current of @total.'),
        'error_message' => t('An error occurred'),
        'finished' => 'content_export_batch_finish',
        'file' => drupal_get_path('module', 'content_export_yaml') . '/drush/content_export_yaml.batch.inc',
      ];

      // Start the batch job.
      batch_set($batch);
      drush_backend_batch_process();
    }
    else {
      drush_print('Content Export Canceled');
    }
  }
  else {
    drush_print('No items are found');
  }
}