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