Skip to content
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

Merged
merged 4 commits into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services/drupal-console/taxonomy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
taxonomy_delete:
class: Drupal\Console\Command\Taxonomy\DeleteCommand
tags:
- { name: console.command }
1 change: 1 addition & 0 deletions config/translations/en/taxonomy.term.delete.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description: 'Delete taxonomy terms from a vocabulary, command takes the VID as argument or all which will delete every term from every vocabulary'
44 changes: 44 additions & 0 deletions src/Command/Taxonomy/DeleteTermCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Style\DrupalStyle;
use Symfony\Component\Console\Input\InputArgument;

/**
* Class DeleteTermCommand.
*
* @package Drupal\eco_migrate
*/
class DeleteTermCommand extends Command {

use ContainerAwareCommandTrait;

use TermDeletionTrait;

/**
* {@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) {

$io = new DrupalStyle($input, $output);

$this->deleteExistingTerms($input->getArgument('vid'), $io);

}

}
55 changes: 55 additions & 0 deletions src/Command/Taxonomy/TermDeletionTrait.php
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 !== 'all'){
$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();
}
}
}

}

}