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 Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\Command;
use Drupal\Core\Entity\EntityTypeRepository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
-use Drupal\Console\Core\Style\DrupalStyle;
class DeleteCommand extends Command
{
- use CommandTrait;
-
/**
* @var EntityTypeRepository
*/
$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,
'entity-id',
InputArgument::REQUIRED,
$this->trans('commands.entity.delete.arguments.entity-id')
- );
+ )->setAliases(['ed']);
}
/**
*/
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);
- $entityType = $io->choice(
+ $entityType = $this->getIo()->choice(
$this->trans('commands.entity.delete.questions.entity-type'),
array_keys($entityTypes)
);
- $entityDefinitionID = $io->choice(
+ $entityDefinitionID = $this->getIo()->choice(
$this->trans('commands.entity.delete.questions.entity-definition-id'),
array_keys($entityTypes[$entityType])
);
$input->setArgument('entity-definition-id', $entityDefinitionID);
}
- if (!$entityID) {
- $entityID = $io->ask(
+ if ($all) {
+ $input->setArgument('entity-id', '-');
+ } elseif (!$entityID) {
+ $entityID = $this->getIo()->ask(
$this->trans('commands.entity.delete.questions.entity-id')
);
$input->setArgument('entity-id', $entityID);
*/
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 ($this->getIo()->confirm(
+ sprintf(
+ $this->trans('commands.entity.delete.messages.confirm-delete-all'),
+ $entityDefinitionID,
+ count($entities)
+ )
+ )
+ ) {
+ $storage->delete($entities);
+ $this->getIo()->success(
+ sprintf(
+ $this->trans('commands.entity.delete.messages.deleted-all'),
+ $entityDefinitionID,
+ count($entities)
+ )
+ );
+ }
+ } else {
+ $entityID = $input->getArgument('entity-id');
+ $storage->load($entityID)->delete();
+ $this->getIo()->success(
+ sprintf(
+ $this->trans('commands.entity.delete.messages.deleted'),
+ $entityDefinitionID,
+ $entityID
+ )
+ );
+ }
} catch (\Exception $e) {
- $io->error($e->getMessage());
+ $this->getIo()->error($e->getMessage());
return 1;
}
-
- $io->success(
- sprintf(
- $this->trans('commands.entity.delete.messages.deleted'),
- $entityDefinitionID,
- $entityID
- )
- );
}
}