Browse Source

consolidated counts to instance property

main
astanley 5 hours ago
parent
commit
e679be969a
  1. 70
      src/Controller/MetadataProfileController.php

70
src/Controller/MetadataProfileController.php

@ -101,6 +101,13 @@ class MetadataProfileController extends ControllerBase {
*/
protected EntityTypeManagerInterface $entityTypeManagerService;
/**
* Cached usage counts keyed by field name.
*
* @var array<string,int>
*/
protected array $usageCounts = [];
public function __construct(
EntityFieldManagerInterface $entity_field_manager,
FieldTypePluginManagerInterface $field_type_plugin_manager,
@ -109,7 +116,7 @@ class MetadataProfileController extends ControllerBase {
FileSystemInterface $file_system,
FileDownloadController $file_download_controller,
Connection $database,
EntityTypeManagerInterface $entity_type_manager // ✅ ADD THIS
EntityTypeManagerInterface $entity_type_manager
) {
$this->entityFieldManager = $entity_field_manager;
$this->fieldTypePluginManager = $field_type_plugin_manager;
@ -285,6 +292,7 @@ class MetadataProfileController extends ControllerBase {
$metadata_profile = [];
$field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeBundleOf, $this->entityBundle);
$this->loadUsageCounts($field_definitions);
foreach ($field_definitions as $field_name => $field_definition) {
$metadata_profile[$field_name] = [
'label' => $field_definition->getLabel(),
@ -977,22 +985,50 @@ class MetadataProfileController extends ControllerBase {
* @return int
* Number of usages.
*/
private function getUsageCount($field_name) {
if (str_starts_with($field_name, 'field_')) {
$connection = $this->database;
$count = $connection->select("node__{$field_name}", 'f')
->countQuery()
->execute()
->fetchField();
return $count;
}
return $this->entityTypeManagerService
->getStorage('node')
->getQuery()
->accessCheck(FALSE)
->condition('type', $this->entityBundle)
->count()
->execute();
protected function getUsageCount(string $field_name): int {
return $this->usageCounts[$field_name] ?? 0;
}
/**
* Preloads usage counts for all configurable fields on the current bundle.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions
* An array of field definitions keyed by field machine name, as returned by
* EntityFieldManagerInterface::getFieldDefinitions().
*
* @return void
*/
protected function loadUsageCounts(array $field_definitions): void {
if (!empty($this->usageCounts)) {
return;
}
foreach ($field_definitions as $field_name => $field_definition) {
// Only configurable fields (skip base fields like title)
if ($field_definition->getFieldStorageDefinition() instanceof \Drupal\Core\Field\BaseFieldDefinition) {
continue;
}
$table = $this->entityTypeBundleOf . '__' . $field_name;
if (!$this->database->schema()->tableExists($table)) {
$this->usageCounts[$field_name] = 0;
continue;
}
try {
$count = $this->database->select($table, 'f')
->countQuery()
->execute()
->fetchField();
$this->usageCounts[$field_name] = (int) $count;
}
catch (\Exception $e) {
$this->usageCounts[$field_name] = 0;
}
}
}
/**

Loading…
Cancel
Save