ContentFieldInstances.php in Site Audit 8.3
File
src/Plugin/SiteAuditCheck/ContentFieldInstances.php
View source
<?php
namespace Drupal\site_audit\Plugin\SiteAuditCheck;
use Drupal\site_audit\Plugin\SiteAuditCheckBase;
class ContentFieldInstances extends SiteAuditCheckBase {
public function getResultFail() {
}
public function getResultInfo() {
$table_rows = [];
foreach ($this->registry->field_instance_counts as $bundle_name => $entity_types) {
foreach ($entity_types as $entity_type => $fields) {
foreach ($fields as $field_name => $count) {
$table_rows[] = [
$entity_type,
$field_name,
$bundle_name,
$count,
];
}
}
}
$header = [
$this
->t('Entity Type'),
$this
->t('Field Name'),
$this
->t('Bundle Name'),
$this
->t('Count'),
];
return [
'#theme' => 'table',
'#class' => 'table-condensed',
'#header' => $header,
'#rows' => $table_rows,
];
}
public function getResultPass() {
}
public function getResultWarn() {
}
public function getAction() {
}
public function calculateScore() {
if (!isset($this->registry->fields)) {
$this
->checkInvokeCalculateScore('content_field_count');
}
$map = \Drupal::service('entity_field.manager')
->getFieldMap();
$this->registry->field_instance_counts = [];
foreach ($map as $entity => $fields) {
$bundle_column_name = \Drupal::service('entity_type.manager')
->getDefinition($entity)
->getKey('bundle');
foreach ($fields as $field => $description) {
if (!in_array($field, array_keys($this->registry->fields))) {
continue;
}
foreach ($description['bundles'] as $bundle) {
if ($description['type'] == 'address') {
$database = \Drupal\Core\Database\Database::getConnection();
$table = $entity . '__' . $field;
$query = $database
->select($table);
$query
->condition($field . '_country_code', NULL, 'IS NOT NULL')
->condition('bundle', $bundle);
$field_count = $query
->countQuery()
->execute()
->fetchField();
}
else {
$query = \Drupal::entityQuery($entity);
if (!empty($bundle_column_name)) {
$query
->condition($bundle_column_name, $bundle);
}
$query
->exists($field)
->count();
$field_count = $query
->execute();
}
$this->registry->field_instance_counts[$bundle][$entity][$field] = $field_count;
}
}
}
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}
}