Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Taxonomy / DeleteTermCommand.php
index 097535d09d7f13860465598c1199b7330ac54ab8..4ec6b38d613cb8dc3ab408d68bb5bc3dd0eb6b23 100644 (file)
@@ -4,13 +4,11 @@ namespace Drupal\Console\Command\Taxonomy;
 
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\Command;
 use Symfony\Component\Console\Input\InputArgument;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\taxonomy\Entity\Term;
 use Drupal\taxonomy\Entity\Vocabulary;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
-use Drupal\Console\Core\Style\DrupalStyle;
 
 /**
  * Class DeleteTermCommand.
@@ -19,8 +17,6 @@ use Drupal\Console\Core\Style\DrupalStyle;
  */
 class DeleteTermCommand extends Command
 {
-    use CommandTrait;
-
     /**
      * The entity_type storage.
      *
@@ -40,62 +36,105 @@ class DeleteTermCommand extends Command
     }
 
     /**
-   * {@inheritdoc}
-   */
+     * {@inheritdoc}
+     */
     protected function configure()
     {
         $this
             ->setName('taxonomy:term:delete')
             ->setDescription($this->trans('commands.taxonomy.term.delete.description'))
-            ->addArgument('vid', InputArgument::REQUIRED);
+            ->addArgument(
+                'vid',
+                InputArgument::REQUIRED
+            )->setAliases(['ttd']);
     }
 
     /**
-   * {@inheritdoc}
-   */
+     * {@inheritdoc}
+     */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $vid = $input->getArgument('vid');
-        $io = new DrupalStyle($input, $output);
 
-        $this->deleteExistingTerms($vid, $io);
+        if ($vid === 'all') {
+            $vid = $vid;
+        } elseif (!in_array($vid, array_keys($this->getVocabularies()))) {
+            $this->getIo()->error(
+                sprintf(
+                    $this->trans('commands.taxonomy.term.delete.messages.invalid-vocabulary'),
+                    $vid
+                )
+            );
+            return;
+        }
+        $this->deleteExistingTerms($vid);
+    }
 
-        return 0;
+    /**
+     * {@inheritdoc}
+     */
+    protected function interact(InputInterface $input, OutputInterface $output)
+    {
+        // --vid argument
+        $vid = $input->getArgument('vid');
+        if (!$vid) {
+            $vid = $this->getIo()->choiceNoList(
+                $this->trans('commands.taxonomy.term.delete.vid'),
+                array_keys($this->getVocabularies())
+            );
+            $input->setArgument('vid', $vid);
+        }
     }
 
     /**
-   * Destroy all existing terms before import
+     * Destroy all existing terms
      *
-   * @param $vid
-   * @param $io
-   */
-    private function deleteExistingTerms($vid = null, DrupalStyle $io)
+     * @param $vid
+     */
+    private function deleteExistingTerms($vid = null)
     {
-        //Load the vid
         $termStorage = $this->entityTypeManager->getStorage('taxonomy_term');
-        $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')
-            ->loadMultiple();
+        //Load all vocabularies
+        $vocabularies = $this->getVocabularies();
 
-        if ($vid !== 'all') {
-            $vid = [$vid];
-        } else {
+        if ($vid === 'all') {
             $vid = array_keys($vocabularies);
+        } else {
+            $vid = [$vid];
         }
 
         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();
+            if (empty($terms)) {
+                $this->getIo()->error(
+                    sprintf(
+                        $this->trans('commands.taxonomy.term.delete.messages.nothing'),
+                        $item
+                    )
+                );
+            } else {
+                foreach ($terms as $term) {
+                    $treal = $termStorage->load($term->tid);
+
+                    if ($treal !== null) {
+                        $this->getIo()->info(
+                            sprintf(
+                                $this->trans('commands.taxonomy.term.delete.messages.processing'),
+                                $term->name
+                            )
+                        );
+                        $treal->delete();
+                    }
                 }
             }
         }
     }
+
+    private function getVocabularies()
+    {
+        return $this->entityTypeManager->getStorage('taxonomy_vocabulary')
+            ->loadMultiple();
+    }
 }