X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FTaxonomy%2FDeleteTermCommand.php;fp=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FTaxonomy%2FDeleteTermCommand.php;h=097535d09d7f13860465598c1199b7330ac54ab8;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/vendor/drupal/console/src/Command/Taxonomy/DeleteTermCommand.php b/vendor/drupal/console/src/Command/Taxonomy/DeleteTermCommand.php new file mode 100644 index 000000000..097535d09 --- /dev/null +++ b/vendor/drupal/console/src/Command/Taxonomy/DeleteTermCommand.php @@ -0,0 +1,101 @@ +entityTypeManager = $entityTypeManager; + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('taxonomy:term:delete') + ->setDescription($this->trans('commands.taxonomy.term.delete.description')) + ->addArgument('vid', InputArgument::REQUIRED); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $vid = $input->getArgument('vid'); + $io = new DrupalStyle($input, $output); + + $this->deleteExistingTerms($vid, $io); + + return 0; + } + + /** + * Destroy all existing terms before import + * + * @param $vid + * @param $io + */ + private function deleteExistingTerms($vid = null, DrupalStyle $io) + { + //Load the vid + $termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); + $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary') + ->loadMultiple(); + + if ($vid !== 'all') { + $vid = [$vid]; + } else { + $vid = array_keys($vocabularies); + } + + foreach ($vid as $item) { + if (!isset($vocabularies[$item])) { + $io->error("Invalid vid: {$item}."); + } + $vocabulary = $vocabularies[$item]; + $terms = $termStorage->loadTree($vocabulary->id()); + + foreach ($terms as $term) { + $treal = $termStorage->load($term->tid); + if ($treal !== null) { + $io->info("Deleting '{$term->name}' and all translations."); + $treal->delete(); + } + } + } + } +}