ContentVocabularies.php in Site Audit 8.3
File
src/Plugin/SiteAuditCheck/ContentVocabularies.php
View source
<?php
namespace Drupal\site_audit\Plugin\SiteAuditCheck;
use Drupal\site_audit\Plugin\SiteAuditCheckBase;
class ContentVocabularies extends SiteAuditCheckBase {
public function getResultFail() {
}
public function getResultInfo() {
if (!isset($this->registry->vocabulary_counts)) {
return $this
->t('The taxonomy module is not enabled.');
}
if (empty($this->registry->vocabulary_counts)) {
if ($this->options['detail']) {
return $this
->t('No vocabularies exist.');
}
return '';
}
$ret_val = '';
$table_rows = [];
foreach ($this->registry->vocabulary_counts as $vocabulary => $count) {
$table_rows[] = [
$vocabulary,
$count,
];
}
$header = [
$this
->t('Vocabulary'),
$this
->t('Terms'),
];
return [
'#theme' => 'table',
'#class' => 'table-condensed',
'#header' => $header,
'#rows' => $table_rows,
];
}
public function getResultPass() {
}
public function getResultWarn() {
return $this
->getResultInfo();
}
public function getAction() {
}
public function calculateScore() {
if (!\Drupal::moduleHandler()
->moduleExists('taxonomy')) {
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}
if (!isset($this->registry->vocabulary_unused)) {
$this->registry->vocabulary_unused = [];
$vocabularies = \Drupal::service('entity_type.bundle.info')
->getBundleInfo("taxonomy_term");
$query = \Drupal::database()
->select('taxonomy_term_field_data');
$query
->addExpression('COUNT(tid)', 'count');
$query
->addField('taxonomy_term_field_data', 'vid');
$query
->orderBy('count', 'DESC');
$query
->groupBy('vid');
$result = $query
->execute();
$this->registry->vocabulary_counts = $this->registry->vocabulary_unused = [];
while ($row = $result
->fetchAssoc()) {
$label = $vocabularies[$row['vid']]['label'];
$this->registry->vocabulary_counts[$label] = $row['count'];
}
foreach ($vocabularies as $vocabulary) {
if (array_search($vocabulary['label'], array_keys($this->registry->vocabulary_counts)) === FALSE) {
$this->registry->vocabulary_unused[] = $vocabulary['label'];
$this->registry->vocabulary_counts[$vocabulary['label']] = 0;
}
}
if (empty($this->registry->vocabulary_counts)) {
$this->abort = TRUE;
}
}
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}
}