entityTypeRepository = $entityTypeRepository; $this->entityTypeManager = $entityTypeManager; parent::__construct(); } /** * {@inheritdoc} */ protected function configure() { $this ->setName('entity:delete') ->setDescription($this->trans('commands.entity.delete.description')) ->addArgument( 'entity-definition-id', InputArgument::REQUIRED, $this->trans('commands.entity.delete.arguments.entity-definition-id') ) ->addArgument( 'entity-id', InputArgument::REQUIRED, $this->trans('commands.entity.delete.arguments.entity-id') ); } /** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $entityDefinitionID = $input->getArgument('entity-definition-id'); $entityID = $input->getArgument('entity-id'); if (!$entityDefinitionID) { $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true); $entityType = $io->choice( $this->trans('commands.entity.delete.questions.entity-type'), array_keys($entityTypes) ); $entityDefinitionID = $io->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( $this->trans('commands.entity.delete.questions.entity-id') ); $input->setArgument('entity-id', $entityID); } } /** * {@inheritdoc} */ 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(); } catch (\Exception $e) { $io->error($e->getMessage()); return 1; } $io->success( sprintf( $this->trans('commands.entity.delete.messages.deleted'), $entityDefinitionID, $entityID ) ); } }