ContentFieldCount.php in Site Audit 8.3
File
src/Plugin/SiteAuditCheck/ContentFieldCount.php
View source
<?php
namespace Drupal\site_audit\Plugin\SiteAuditCheck;
use Drupal\site_audit\Plugin\SiteAuditCheckBase;
class ContentFieldCount extends SiteAuditCheckBase {
public function getResultFail() {
return $this
->t('There are no fields available!');
}
public function getResultInfo() {
$table_rows = [];
foreach ($this->registry->fields as $field_name => $type) {
$table_rows[] = [
$field_name,
$type,
];
}
$header = [
$this
->t('Name'),
$this
->t('Type'),
];
return [
'#theme' => 'table',
'#class' => 'table-condensed',
'#header' => $header,
'#rows' => $table_rows,
'#title' => $this
->t('There are @count total fields.', [
'@count' => count($this->registry->fields),
]),
];
}
public function getResultPass() {
}
public function getResultWarn() {
return $this
->t('There are @count total fields, which is higher than average', [
'@count' => count($this->registry->fields),
]);
}
public function getAction() {
if ($this
->getScore() == SiteAuditCheckBase::AUDIT_CHECK_SCORE_FAIL) {
return $this
->t('Consider disabling the field module.');
}
}
public function calculateScore() {
if (!isset($this->registry->fields)) {
$map = \Drupal::service('entity_field.manager')
->getFieldMap();
$this->registry->fields = [];
$this->registry->default_fields = [
'body',
'comment_body',
];
foreach ($map as $fields) {
foreach ($fields as $field => $description) {
if (preg_match('/^field\\_/', $field) || in_array($field, $this->registry->default_fields)) {
$this->registry->fields[$field] = $description['type'];
}
}
}
}
if (count($this->registry->fields) == 0) {
$this->abort;
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_FAIL;
}
elseif (count($this->registry->fields) > 75) {
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_WARN;
}
return SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}
}