From 782a2bc14405bbd08d2e7fe1d69dda78291935d4 Mon Sep 17 00:00:00 2001 From: Christoph Burschka Date: Tue, 5 Dec 2017 10:39:28 +0100 Subject: [PATCH] [entity:delete] Add --all option. (#3599) (#3604) * [entity:delete] Add --all option. (#3599) * [entity:delete] Confirm --all deletion. --- src/Command/Entity/DeleteCommand.php | 54 ++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Command/Entity/DeleteCommand.php b/src/Command/Entity/DeleteCommand.php index f9b1ecbb4..eacbfbfb1 100644 --- a/src/Command/Entity/DeleteCommand.php +++ b/src/Command/Entity/DeleteCommand.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Drupal\Console\Core\Command\Command; use Drupal\Core\Entity\EntityTypeRepository; @@ -48,6 +49,12 @@ protected function configure() $this ->setName('entity:delete') ->setDescription($this->trans('commands.entity.delete.description')) + ->addOption( + 'all', + NULL, + InputOption::VALUE_NONE, + $this->trans('commands.entity.delete.options.all') + ) ->addArgument( 'entity-definition-id', InputArgument::REQUIRED, @@ -68,6 +75,7 @@ protected function interact(InputInterface $input, OutputInterface $output) $io = new DrupalStyle($input, $output); $entityDefinitionID = $input->getArgument('entity-definition-id'); $entityID = $input->getArgument('entity-id'); + $all = $input->getOption('all'); if (!$entityDefinitionID) { $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true); @@ -85,7 +93,10 @@ protected function interact(InputInterface $input, OutputInterface $output) $input->setArgument('entity-definition-id', $entityDefinitionID); } - if (!$entityID) { + if ($all) { + $input->setArgument('entity-id', '-'); + } + elseif (!$entityID) { $entityID = $io->ask( $this->trans('commands.entity.delete.questions.entity-id') ); @@ -101,22 +112,45 @@ protected function execute(InputInterface $input, OutputInterface $output) $io = new DrupalStyle($input, $output); $entityDefinitionID = $input->getArgument('entity-definition-id'); - $entityID = $input->getArgument('entity-id'); try { - $this->entityTypeManager->getStorage($entityDefinitionID)->load($entityID)->delete(); + $storage = $this->entityTypeManager->getStorage($entityDefinitionID); + + if ($input->getOption('all')) { + $entities = $storage->loadMultiple(); + if ($io->confirm(sprintf( + $this->trans('commands.entity.delete.messages.confirm-delete-all'), + $entityDefinitionID, + count($entities) + )) + ) { + $storage->delete($entities); + $io->success( + sprintf( + $this->trans('commands.entity.delete.messages.deleted-all'), + $entityDefinitionID, + count($entities) + ) + ); + } + } + else { + $entityID = $input->getArgument('entity-id'); + $storage->load($entityID)->delete(); + $io->success( + sprintf( + $this->trans('commands.entity.delete.messages.deleted'), + $entityDefinitionID, + $entityID + ) + ); + } } catch (\Exception $e) { $io->error($e->getMessage()); return 1; } - $io->success( - sprintf( - $this->trans('commands.entity.delete.messages.deleted'), - $entityDefinitionID, - $entityID - ) - ); + } }