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 2 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
2 changes: 2 additions & 0 deletions config/translations/en/taxo.delete.yml
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'
41 changes: 41 additions & 0 deletions src/Command/Taxonomy/TaxoDeleteAllCommand.php
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;
Copy link
Member

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

use Symfony\Component\Console\Command\Command as BaseCommand;

as this

+use Symfony\Component\Console\Command\Command;

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 {
Copy link
Member

@jmolivas jmolivas Aug 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twhiston Update this and the other command from

extends BaseCommand {

to

extends Command {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update class name from TaxoDeleteAllCommand to DeleteAllCommand since the command is already on a directory Taxonomy

You can see how the status commands are names on this image:
namespace-taxonomy


use ContainerAwareCommandTrait;

use TermDeletionTrait;

/**
* {@inheritdoc}
*/
protected function configure() {
$this
->setName('taxo:delete:all')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set command name as:

->setName('taxonomy:delete:all')

->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);
}

}
42 changes: 42 additions & 0 deletions src/Command/Taxonomy/TaxoDeleteCommand.php
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);
}

}
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 !== 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();
}
}
}

}

}