3 namespace Drupal\Console\Command\Taxonomy;
5 use Symfony\Component\Console\Input\InputInterface;
6 use Symfony\Component\Console\Output\OutputInterface;
7 use Symfony\Component\Console\Command\Command;
8 use Symfony\Component\Console\Input\InputArgument;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\taxonomy\Entity\Term;
11 use Drupal\taxonomy\Entity\Vocabulary;
12 use Drupal\Console\Core\Command\Shared\CommandTrait;
13 use Drupal\Console\Core\Style\DrupalStyle;
16 * Class DeleteTermCommand.
18 * @package Drupal\eco_migrate
20 class DeleteTermCommand extends Command
25 * The entity_type storage.
27 * @var EntityTypeManagerInterface
29 protected $entityTypeManager;
32 * InfoCommand constructor.
34 * @param EntityTypeManagerInterface $entityTypeManager
36 public function __construct(EntityTypeManagerInterface $entityTypeManager)
38 $this->entityTypeManager = $entityTypeManager;
39 parent::__construct();
45 protected function configure()
48 ->setName('taxonomy:term:delete')
49 ->setDescription($this->trans('commands.taxonomy.term.delete.description'))
50 ->addArgument('vid', InputArgument::REQUIRED);
56 protected function execute(InputInterface $input, OutputInterface $output)
58 $vid = $input->getArgument('vid');
59 $io = new DrupalStyle($input, $output);
61 $this->deleteExistingTerms($vid, $io);
67 * Destroy all existing terms before import
72 private function deleteExistingTerms($vid = null, DrupalStyle $io)
75 $termStorage = $this->entityTypeManager->getStorage('taxonomy_term');
76 $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')
82 $vid = array_keys($vocabularies);
85 foreach ($vid as $item) {
86 if (!isset($vocabularies[$item])) {
87 $io->error("Invalid vid: {$item}.");
89 $vocabulary = $vocabularies[$item];
90 $terms = $termStorage->loadTree($vocabulary->id());
92 foreach ($terms as $term) {
93 $treal = $termStorage->load($term->tid);
94 if ($treal !== null) {
95 $io->info("Deleting '{$term->name}' and all translations.");