views_entity_field_label

views_entity_field_label($entity_type, $field_name)

Returns the label of a certain field.

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

File

core/modules/views/views.views.inc, line 247
Provide views data that isn't tied to any other module.

Code

function views_entity_field_label($entity_type, $field_name) {
  $label_counter = array();
  $all_labels = array();
  // Count the amount of fields per label per field storage.
  foreach (array_keys(\Drupal::entityManager()->getBundleInfo($entity_type)) as $bundle) {
    $bundle_fields = array_filter(\Drupal::entityManager()->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 array($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 array($label_counter[0], $all_labels);
}
doc_Drupal
2016-10-29 09:55:36
Comments
Leave a Comment

Please login to continue.