Skip to content
Snippets Groups Projects

A site maintainer can bulk delete all terms in a vocabulary that are referenced by N or fewer nodes

12 files
+ 634
53
Compare changes
  • Side-by-side
  • Inline
Files
12
@@ -2,9 +2,9 @@
namespace Drupal\cleantaxonomy\Controller;
use Drupal\cleantaxonomy\TermsUsageCounterInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Database\Connection;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -12,30 +12,29 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* Provides route responses for the cleantaxonomy module.
*/
class TaxonomyList extends ControllerBase {
/**
* The database service.
* The term usage counter service.
*
* @var \Drupal\Core\Database\Connection
* @var \Drupal\cleantaxonomy\TermsUsageCounter
*/
protected $database;
protected $termsUsageCounter;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database')
$container->get('cleantaxonomy.terms_usage_counter')
);
}
/**
* Constructs a cleantaxonomy object.
*
* @param \Drupal\Core\Database\Connection $database
* A database connection.
*/
public function __construct(Connection $database) {
$this->database = $database;
public function __construct(TermsUsageCounterInterface $termsUsageCounter) {
$this->termsUsageCounter = $termsUsageCounter;
}
/**
@@ -45,58 +44,36 @@ class TaxonomyList extends ControllerBase {
* A tabular format of the terms list.
*/
public function taxonomytermsList() {
$terms = $this->termsUsageCounter->getTermsWithUsage();
return $this::termsTable($terms);
}
public static function termsTable($terms) {
$header = [
$this->t('Term ID'),
$this->t('Taxonomy Term'),
$this->t('Vocabulary'),
t('Term ID'),
t('Taxonomy Term'),
t('Vocabulary'),
[
'data' => $this->t('No. of nodes attached'),
'data' => t('No. of nodes attached'),
'sort' => 'desc',
],
[
'data' => $this->t('Operation'),
'data' => t('Operation'),
'colspan' => '4',
],
];
$id_name = $this->database->select('taxonomy_term_field_data')
->fields('taxonomy_term_field_data', ['tid', 'name', 'vid'])
->execute();
$id_name_records = $id_name->fetchAll();
$id_name_values = [];
$size = 0;
foreach ($id_name_records as $id_name_record) {
$id_name_values[$id_name_record->tid] = $id_name_record;
$size++;
}
$node_count = $this->database->select('taxonomy_index', 'x')
->fields('x', ['tid']);
$node_count->addExpression('COUNT(x.nid)', 'n');
$node_count->groupBy('x.tid');
$data = $node_count->execute();
$node_count_records = $data->fetchAll(\PDO::FETCH_ASSOC);
foreach ($node_count_records as $node_count_record) {
$tags[$node_count_record['tid']]['count'] = $node_count_record['n'];
}
foreach ($id_name_values as $id_name_value) {
$tags[$id_name_value->tid]['name'] = $id_name_value->name;
$tags[$id_name_value->tid]['tid'] = $id_name_value->tid;
$tags[$id_name_value->tid]['vid'] = $id_name_value->vid;
if (!$tags[$id_name_value->tid]['count']) {
$tags[$id_name_value->tid]['count'] = '0';
}
}
$rows = [];
foreach ($tags as $value) {
foreach ($terms as $value) {
$rows[] = [
'data' => [
$this->t('@tid', ['@tid' => $value['tid']]),
$this->t('@name', ['@name' => $value['name']]),
$this->t('@vid', ['@vid' => $value['vid']]),
$this->t('@count', ['@count' => $value['count']]),
Link::fromTextAndUrl($this->t('View'), Url::fromUri('internal:/taxonomy/term/' . $value['tid'], [$value['tid']])),
Link::fromTextAndUrl($this->t('Edit'), Url::fromUri('internal:/taxonomy/term/' . $value['tid'] . '/edit')),
Link::fromTextAndUrl($this->t('Delete'), Url::fromUri('internal:/taxonomy/term/' . $value['tid'] . '/delete')),
Link::fromTextAndUrl($this->t('Replace'), new Url('cleantaxonomy.replace', ['tid' => $value['tid']])),
t('@tid', ['@tid' => $value['tid']]),
t('@name', ['@name' => $value['name']]),
t('@vid', ['@vid' => $value['vid']]),
t('@count', ['@count' => $value['count']]),
Link::fromTextAndUrl(t('View'), Url::fromUri('internal:/taxonomy/term/' . $value['tid'], [$value['tid']])),
Link::fromTextAndUrl(t('Edit'), Url::fromUri('internal:/taxonomy/term/' . $value['tid'] . '/edit')),
Link::fromTextAndUrl(t('Delete'), Url::fromUri('internal:/taxonomy/term/' . $value['tid'] . '/delete')),
Link::fromTextAndUrl(t('Replace'), new Url('cleantaxonomy.replace', ['tid' => $value['tid']])),
],
];
}
@@ -104,7 +81,7 @@ class TaxonomyList extends ControllerBase {
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t('No taxonomy terms available.'),
'#empty' => t('No taxonomy terms available.'),
];
$build['admin_cleantaxonomy_list_pager_pager'] = ['#type' => 'pager'];
return $build;
Loading