You are here

protected function ExportCommand::interact in Commerce Demo 8.2

Same name and namespace in other branches
  1. 8 src/Command/ExportCommand.php \Drupal\commerce_demo\Command\ExportCommand::interact()

File

src/Command/ExportCommand.php, line 113

Class

ExportCommand
Provides a command for exporting content entities.

Namespace

Drupal\commerce_demo\Command

Code

protected function interact(InputInterface $input, OutputInterface $output) {
  $helper = $this
    ->getHelper('question');
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $entity_types = array_filter($entity_types, function (EntityType $entity_type) {
    return $entity_type
      ->entityClassImplements(ContentEntityInterface::class);
  });
  $entity_types = array_map(function (EntityType $entity_type) {
    return $entity_type
      ->getLabel();
  }, $entity_types);

  // --entity_type argument.
  $entity_type_id = $input
    ->getArgument('entity_type');
  if (!$entity_type_id) {
    $question = new ChoiceQuestion($this
      ->trans('commands.commerce_demo.export.questions.entity_type'), $entity_types);
    $entity_type_id = $helper
      ->ask($input, $output, $question);
  }
  $input
    ->setArgument('entity_type', $entity_type_id);

  // --bundle argument.
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($entity_type_id);
  $bundles = array_map(function ($bundle) {
    return $bundle['label'];
  }, $bundles);
  if (count($bundles) === 1) {
    $bundle_keys = array_keys($bundles);
    $bundle = reset($bundle_keys);
  }
  else {
    $bundle = $input
      ->getArgument('bundle');
    if (!$bundle) {
      $question = new ChoiceQuestion($this
        ->trans('commands.commerce_demo.export.questions.bundle'), $bundles);
      $bundle = $helper
        ->ask($input, $output, $question);
    }
  }
  $input
    ->setArgument('bundle', $bundle);
}