function _diff_field_label in Diff 8
Returns the label of a certain field.
Therefore it looks up in all bundles to find the most used field.
Parameters
string $entity_type: The entity type id.
string $field_name: The field name.
Return value
array Array of labels used for the field sorted by the most used.
1 call to _diff_field_label()
- FieldsSettingsForm::buildFieldRow in src/
Form/ FieldsSettingsForm.php - Builds a row for the table. Each row corresponds to a field type.
File
- ./
diff.module, line 54 - This is the diff module to compare revisions.
Code
function _diff_field_label($entity_type, $field_name) {
$labels = [];
// Count the amount of instances of each label per field storage.
$bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type);
$field_manager = \Drupal::service('entity_field.manager');
foreach (array_keys($bundles) as $bundle) {
$bundle_instances = $field_manager
->getFieldDefinitions($entity_type, $bundle);
if (isset($bundle_instances[$field_name])) {
$instance = $bundle_instances[$field_name];
$label = (string) $instance
->getLabel();
$labels[$label] = isset($labels[$label]) ? ++$labels[$label] : 1;
}
}
if (empty($labels)) {
// Return the original field name if there is no other label found.
return [
$field_name,
];
}
// Return the labels sorted by the most used.
arsort($labels);
return array_keys($labels);
}