You are here

public function ContentVocabularies::calculateScore in Site Audit 8.3

.

Overrides SiteAuditCheckBase::calculateScore

File

src/Plugin/SiteAuditCheck/ContentVocabularies.php, line 80

Class

ContentVocabularies
Provides the ContentVocabularies Check.

Namespace

Drupal\site_audit\Plugin\SiteAuditCheck

Code

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'];
    }

    // Check for unused vocabularies.
    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;
      }
    }

    // No need to check for unused vocabularies if there aren't any.
    if (empty($this->registry->vocabulary_counts)) {
      $this->abort = TRUE;
    }
  }
  return SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}