function views_entity_field_label

Same name in other branches
  1. 9 core/modules/views/views.views.inc \views_entity_field_label()
  2. 10 core/modules/views/views.views.inc \views_entity_field_label()
  3. 11.x core/modules/views/views.views.inc \views_entity_field_label()

Returns the label of a certain field.

Therefore it looks up in all bundles to find the most used field.

5 calls to views_entity_field_label()
file_field_views_data_views_data_alter in core/modules/file/file.views.inc
Implements hook_field_views_data_views_data_alter().
hook_field_views_data_alter in core/modules/views/views.api.php
Alter the Views data for a single Field API field.
hook_field_views_data_views_data_alter in core/modules/views/views.api.php
Alter the Views data on a per field basis.
image_field_views_data_views_data_alter in core/modules/image/image.views.inc
Implements hook_field_views_data_views_data_alter().
views_field_default_views_data in core/modules/views/views.views.inc
Default views data implementation for a field.

File

core/modules/views/views.views.inc, line 255

Code

function views_entity_field_label($entity_type, $field_name) {
    $label_counter = [];
    $all_labels = [];
    // Count the amount of fields per label per field storage.
    $entity_field_manager = \Drupal::service('entity_field.manager');
    foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type)) as $bundle) {
        $bundle_fields = array_filter($entity_field_manager->getFieldDefinitions($entity_type, $bundle), function ($field_definition) {
            return $field_definition instanceof FieldConfigInterface;
        });
        if (isset($bundle_fields[$field_name])) {
            $field = $bundle_fields[$field_name];
            $label = $field->getLabel();
            $label_counter[$label] = isset($label_counter[$label]) ? ++$label_counter[$label] : 1;
            $all_labels[$label] = TRUE;
        }
    }
    if (empty($label_counter)) {
        return [
            $field_name,
            $all_labels,
        ];
    }
    // Sort the field labels by it most used label and return the most used one.
    // If the counts are equal, sort by the label to ensure the result is
    // deterministic.
    uksort($label_counter, function ($a, $b) use ($label_counter) {
        if ($label_counter[$a] === $label_counter[$b]) {
            return strcmp($a, $b);
        }
        return $label_counter[$a] > $label_counter[$b] ? -1 : 1;
    });
    $label_counter = array_keys($label_counter);
    return [
        $label_counter[0],
        $all_labels,
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.