Skip to content

Commit

Permalink
[entity:delete] Add --all option. (#3599) (#3604)
Browse files Browse the repository at this point in the history
* [entity:delete] Add --all option. (#3599)

* [entity:delete] Confirm --all deletion.
  • Loading branch information
cburschka authored and jmolivas committed Dec 5, 2017
1 parent c8f72d1 commit 782a2bc
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions src/Command/Entity/DeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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')
);
Expand All @@ -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
)
);

}
}

0 comments on commit 782a2bc

Please sign in to comment.