4 * Contains \Drupal\Console\Command\Entity\DeleteCommand.
7 namespace Drupal\Console\Command\Entity;
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Entity\EntityTypeRepository;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
17 class DeleteCommand extends Command
20 * @var EntityTypeRepository
22 protected $entityTypeRepository;
25 * @var EntityTypeManagerInterface
27 protected $entityTypeManager;
30 * DeleteCommand constructor.
32 * @param EntityTypeRepository $entityTypeRepository
33 * @param EntityTypeManagerInterface $entityTypeManager
35 public function __construct(
36 EntityTypeRepository $entityTypeRepository,
37 EntityTypeManagerInterface $entityTypeManager
39 $this->entityTypeRepository = $entityTypeRepository;
40 $this->entityTypeManager = $entityTypeManager;
41 parent::__construct();
46 protected function configure()
49 ->setName('entity:delete')
50 ->setDescription($this->trans('commands.entity.delete.description'))
54 InputOption::VALUE_NONE,
55 $this->trans('commands.entity.delete.options.all')
58 'entity-definition-id',
59 InputArgument::REQUIRED,
60 $this->trans('commands.entity.delete.arguments.entity-definition-id')
64 InputArgument::REQUIRED,
65 $this->trans('commands.entity.delete.arguments.entity-id')
66 )->setAliases(['ed']);
72 protected function interact(InputInterface $input, OutputInterface $output)
74 $entityDefinitionID = $input->getArgument('entity-definition-id');
75 $entityID = $input->getArgument('entity-id');
76 $all = $input->getOption('all');
78 if (!$entityDefinitionID) {
79 $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true);
81 $entityType = $this->getIo()->choice(
82 $this->trans('commands.entity.delete.questions.entity-type'),
83 array_keys($entityTypes)
86 $entityDefinitionID = $this->getIo()->choice(
87 $this->trans('commands.entity.delete.questions.entity-definition-id'),
88 array_keys($entityTypes[$entityType])
91 $input->setArgument('entity-definition-id', $entityDefinitionID);
95 $input->setArgument('entity-id', '-');
96 } elseif (!$entityID) {
97 $entityID = $this->getIo()->ask(
98 $this->trans('commands.entity.delete.questions.entity-id')
100 $input->setArgument('entity-id', $entityID);
107 protected function execute(InputInterface $input, OutputInterface $output)
109 $entityDefinitionID = $input->getArgument('entity-definition-id');
112 $storage = $this->entityTypeManager->getStorage($entityDefinitionID);
114 if ($input->getOption('all')) {
115 $entities = $storage->loadMultiple();
116 if ($this->getIo()->confirm(
118 $this->trans('commands.entity.delete.messages.confirm-delete-all'),
124 $storage->delete($entities);
125 $this->getIo()->success(
127 $this->trans('commands.entity.delete.messages.deleted-all'),
134 $entityID = $input->getArgument('entity-id');
135 $storage->load($entityID)->delete();
136 $this->getIo()->success(
138 $this->trans('commands.entity.delete.messages.deleted'),
144 } catch (\Exception $e) {
145 $this->getIo()->error($e->getMessage());