function ContentExportCommand::execute

File

core/lib/Drupal/Core/DefaultContent/ContentExportCommand.php, line 58

Class

ContentExportCommand
Exports content entities in YAML format.

Namespace

Drupal\Core\DefaultContent

Code

protected function execute(InputInterface $input, OutputInterface $output) : int {
  $io = new SymfonyStyle($input, $output);
  $container = $this->boot()
    ->getContainer();
  $entity_type_id = $input->getArgument('entity_type_id');
  $entity_id = $input->getArgument('entity_id');
  $bundles = $input->getOption('bundle');
  $entity_type_manager = $container->get(EntityTypeManagerInterface::class);
  if (!$entity_type_manager->hasDefinition($entity_type_id)) {
    $io->error("The entity type \"{$entity_type_id}\" does not exist.");
    return 1;
  }
  if (!$entity_type_manager->getDefinition($entity_type_id)
    ->entityClassImplements(ContentEntityInterface::class)) {
    $io->error("{$entity_type_id} is not a content entity type.");
    return 1;
  }
  // Confirm that all specified bundles exist.
  if ($bundles) {
    $unknown_bundles = array_diff($bundles, array_keys($container->get(EntityTypeBundleInfoInterface::class)
      ->getBundleInfo($entity_type_id)));
    if ($unknown_bundles) {
      $io->error("These bundles do not exist on the {$entity_type_id} entity type: " . implode(', ', $unknown_bundles));
      return 1;
    }
  }
  $dir = $input->getOption('dir');
  $with_dependencies = $input->getOption('with-dependencies');
  $exporter = $container->get(Exporter::class);
  // If we're going to export multiple entities, or a single entity with its
  // dependencies, require the `--dir` option.
  if (empty($dir) && (empty($entity_id) || $with_dependencies)) {
    throw new RuntimeException('The --dir option is required to export multiple entities, or a single entity with its dependencies.');
  }
  $count = 0;
  $storage = $entity_type_manager->getStorage($entity_type_id);
  foreach ($this->loadEntities($storage, $entity_id, $bundles) as $entity) {
    if ($with_dependencies) {
      $count += $exporter->exportWithDependencies($entity, $dir);
    }
    elseif ($dir) {
      $exporter->exportToFile($entity, $dir);
      $count++;
    }
    else {
      $io->write((string) $exporter->export($entity));
      return 0;
    }
  }
  // If we were trying to export a specific entity and it didn't get exported,
  // that's an error.
  if ($entity_id && $count === 0) {
    $io->error("{$entity_type_id} {$entity_id} does not exist.");
    if ($bundles) {
      $io->caution('Maybe this entity is not one of the specified bundles: ' . implode(', ', $bundles));
    }
    return 1;
  }
  $file_system = $container->get(FileSystemInterface::class);
  $message = (string) $this->formatPlural($count, 'One entity was exported to @dir.', '@count entities were exported to @dir.', [
    '@dir' => $file_system->realpath($dir),
  ]);
  $io->success($message);
  return 0;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.