Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Taxonomy / DeleteTermCommand.php
1 <?php
2
3 namespace Drupal\Console\Command\Taxonomy;
4
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;
14
15 /**
16  * Class DeleteTermCommand.
17  *
18  * @package Drupal\eco_migrate
19  */
20 class DeleteTermCommand extends Command
21 {
22     use CommandTrait;
23
24     /**
25      * The entity_type storage.
26      *
27      * @var EntityTypeManagerInterface
28      */
29     protected $entityTypeManager;
30
31     /**
32      * InfoCommand constructor.
33      *
34      * @param EntityTypeManagerInterface $entityTypeManager
35      */
36     public function __construct(EntityTypeManagerInterface $entityTypeManager)
37     {
38         $this->entityTypeManager = $entityTypeManager;
39         parent::__construct();
40     }
41
42     /**
43    * {@inheritdoc}
44    */
45     protected function configure()
46     {
47         $this
48             ->setName('taxonomy:term:delete')
49             ->setDescription($this->trans('commands.taxonomy.term.delete.description'))
50             ->addArgument('vid', InputArgument::REQUIRED);
51     }
52
53     /**
54    * {@inheritdoc}
55    */
56     protected function execute(InputInterface $input, OutputInterface $output)
57     {
58         $vid = $input->getArgument('vid');
59         $io = new DrupalStyle($input, $output);
60
61         $this->deleteExistingTerms($vid, $io);
62
63         return 0;
64     }
65
66     /**
67    * Destroy all existing terms before import
68      *
69    * @param $vid
70    * @param $io
71    */
72     private function deleteExistingTerms($vid = null, DrupalStyle $io)
73     {
74         //Load the vid
75         $termStorage = $this->entityTypeManager->getStorage('taxonomy_term');
76         $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')
77             ->loadMultiple();
78
79         if ($vid !== 'all') {
80             $vid = [$vid];
81         } else {
82             $vid = array_keys($vocabularies);
83         }
84
85         foreach ($vid as $item) {
86             if (!isset($vocabularies[$item])) {
87                 $io->error("Invalid vid: {$item}.");
88             }
89             $vocabulary = $vocabularies[$item];
90             $terms = $termStorage->loadTree($vocabulary->id());
91
92             foreach ($terms as $term) {
93                 $treal = $termStorage->load($term->tid);
94                 if ($treal !== null) {
95                     $io->info("Deleting '{$term->name}' and all translations.");
96                     $treal->delete();
97                 }
98             }
99         }
100     }
101 }