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\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Core\Entity\EntityTypeRepository;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
18 class DeleteCommand extends Command
23 * @var EntityTypeRepository
25 protected $entityTypeRepository;
28 * @var EntityTypeManagerInterface
30 protected $entityTypeManager;
33 * DeleteCommand constructor.
35 * @param EntityTypeRepository $entityTypeRepository
36 * @param EntityTypeManagerInterface $entityTypeManager
38 public function __construct(
39 EntityTypeRepository $entityTypeRepository,
40 EntityTypeManagerInterface $entityTypeManager
42 $this->entityTypeRepository = $entityTypeRepository;
43 $this->entityTypeManager = $entityTypeManager;
44 parent::__construct();
49 protected function configure()
52 ->setName('entity:delete')
53 ->setDescription($this->trans('commands.entity.delete.description'))
55 'entity-definition-id',
56 InputArgument::REQUIRED,
57 $this->trans('commands.entity.delete.arguments.entity-definition-id')
61 InputArgument::REQUIRED,
62 $this->trans('commands.entity.delete.arguments.entity-id')
69 protected function interact(InputInterface $input, OutputInterface $output)
71 $io = new DrupalStyle($input, $output);
72 $entityDefinitionID = $input->getArgument('entity-definition-id');
73 $entityID = $input->getArgument('entity-id');
75 if (!$entityDefinitionID) {
76 $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true);
78 $entityType = $io->choice(
79 $this->trans('commands.entity.delete.questions.entity-type'),
80 array_keys($entityTypes)
83 $entityDefinitionID = $io->choice(
84 $this->trans('commands.entity.delete.questions.entity-definition-id'),
85 array_keys($entityTypes[$entityType])
88 $input->setArgument('entity-definition-id', $entityDefinitionID);
93 $this->trans('commands.entity.delete.questions.entity-id')
95 $input->setArgument('entity-id', $entityID);
102 protected function execute(InputInterface $input, OutputInterface $output)
104 $io = new DrupalStyle($input, $output);
106 $entityDefinitionID = $input->getArgument('entity-definition-id');
107 $entityID = $input->getArgument('entity-id');
110 $this->entityTypeManager->getStorage($entityDefinitionID)->load($entityID)->delete();
111 } catch (\Exception $e) {
112 $io->error($e->getMessage());
119 $this->trans('commands.entity.delete.messages.deleted'),