-
-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#2542 Add taxonomy delete command. #2543
Changes from 2 commits
bbae3d7
c292b23
76eb4c8
7a4299e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: 'Delete all taxonomy terms from a vocabulary' | ||
all-description: 'Delete all taxonomy terms from every vocabulary' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Drupal\Console\Command\Taxonomy; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Command\Command as BaseCommand; | ||
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait; | ||
use Drupal\Console\Style\DrupalStyle; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
/** | ||
* Class TaxoDeleteCommand. | ||
* | ||
* @package Drupal\eco_migrate | ||
*/ | ||
class TaxoDeleteAllCommand extends BaseCommand { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
use ContainerAwareCommandTrait; | ||
|
||
use TermDeletionTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() { | ||
$this | ||
->setName('taxo:delete:all') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please set command name as:
|
||
->setDescription($this->trans('commands.taxo.delete.all-description')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$io = new DrupalStyle($input, $output); | ||
|
||
$this->deleteExistingTerms(null,$io); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Drupal\Console\Command\Taxonomy; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Command\Command as BaseCommand; | ||
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait; | ||
use Drupal\Console\Style\DrupalStyle; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
/** | ||
* Class TaxoDeleteCommand. | ||
* | ||
* @package Drupal\eco_migrate | ||
*/ | ||
class TaxoDeleteCommand extends BaseCommand { | ||
|
||
use ContainerAwareCommandTrait; | ||
|
||
use TermDeletionTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() { | ||
$this | ||
->setName('taxo:delete') | ||
->setDescription($this->trans('commands.taxo.delete.description')) | ||
->addArgument('vid',InputArgument::REQUIRED); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$io = new DrupalStyle($input, $output); | ||
|
||
$this->deleteExistingTerms($input->getArgument('vid'),$io); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: twhiston | ||
* Date: 28/07/16 | ||
* Time: 10:53 | ||
*/ | ||
|
||
namespace Drupal\Console\Command\Taxonomy; | ||
|
||
use Drupal\taxonomy\Entity\Term; | ||
use Drupal\taxonomy\Entity\Vocabulary; | ||
|
||
|
||
trait TermDeletionTrait { | ||
|
||
|
||
/** | ||
* Destroy all existing terms before import | ||
* @param $vid | ||
* @throws \Exception | ||
*/ | ||
private function deleteExistingTerms($vid = null,$io) | ||
{ | ||
//Load the vid | ||
$vocabularies = Vocabulary::loadMultiple(); | ||
|
||
if($vid !== null){ | ||
$vid = [$vid]; | ||
} else { | ||
$vid = array_keys($vocabularies); | ||
} | ||
|
||
foreach ($vid as $item) { | ||
if (!isset($vocabularies[$item])) { | ||
throw new \Exception("vid {$item} does not exist"); | ||
} | ||
$selected_vocab = $vocabularies[$item]; | ||
$terms = \Drupal::getContainer() | ||
->get('entity.manager') | ||
->getStorage('taxonomy_term') | ||
->loadTree($selected_vocab->id()); | ||
|
||
foreach ($terms as $term) { | ||
$treal = Term::load($term->tid); | ||
if($treal !== null){ | ||
$io->info("Deleting {$term->name} and all translations"); | ||
$treal->delete(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twhiston There is no longer need to register this alias. Please update this line
as this