Follow-up from #2225739: Remove usage of field_info_instances(), use EntityManager::getFieldDefinitions() instead.

This function no more needed because field has getLabel() method

$ git grep field_views_field_label
core/modules/field/field.views.inc:80:function field_views_field_label($entity_type, $field_name) {
core/modules/field/field.views.inc:185:  list($label, $all_labels) = field_views_field_label($entity_type_id, $field_name);
core/modules/file/file.views.inc:510:  list($label) = field_views_field_label($entity_type_id, $field_name);
core/modules/image/image.views.inc:45:  list($label) = field_views_field_label($entity_type_id, $field_name);
core/modules/taxonomy/taxonomy.views.inc:443:  list($label) = field_views_field_label($entity_type_id, $field_name);

Only field.views.inc:185 needs some clean-up

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Issue tags: +VDC
Berdir’s picture

Yes, it is needed.

Fields don't have a label, their getLabel() implementation returns the field name.

The views function attempts to display the best label by looking at the field instances for that field and picks the most frequently looked label.

Jalandhar’s picture

Assigned: Unassigned » Jalandhar
Status: Active » Needs review
FileSize
736 bytes

Updating the patch which fixes the issue(Used get label method instead of field_views_field_label() in field.views.inc:185). Please review it.

Status: Needs review » Needs work

The last submitted patch, 3: drupal-remove_field_views_field_label-2231863-3.patch, failed testing.

Jalandhar’s picture

Status: Needs work » Needs review
FileSize
4.89 KB

Updating the patch with required changes. (Removed field_views_field_label() and replaced its function calls with getLabel() in all occurrences as said in the description of issue.)

Please review.

mashermike’s picture

Status: Needs review » Needs work

The last submitted patch, 5: drupal8-remove_field_views_field_label-2231863-5.patch, failed testing.

mashermike’s picture

This patch no longer applies because in commit cda250396e4f501328ddbd5b69dd971bef2d70c2 the field_views_field_label function was modified. This patch moves that block of code to a new location. If this patch is going to be rerolled that modification needs to be ported to the new location in the patch.

Jalandhar’s picture

Status: Needs work » Needs review
FileSize
5.09 KB

Updating the patch with reroll. Please review.

Berdir’s picture

See #2, I think this issue is wrong and should be won't fixed, we still need this function.

Berdir’s picture

That is, unless we want to move the logic into FieldConfig::getLabel(), but I'm not sure about that. It would be useful because other places need this too, for example comment.module has similar code snippet but only looks at the first one.

andypost’s picture

dawehner’s picture

+++ b/core/modules/field/field.views.inc
@@ -74,35 +74,6 @@ function _field_views_is_sql_entity_type(FieldConfigInterface $field) {
- * Therefore it looks up in all bundles to find the most used instance.
- */
-function field_views_field_label($entity_type, $field_name) {
-  $label_counter = array();
-  $all_labels = array();
-  // Count the amount of instances per label per field.
-  foreach (array_keys(\Drupal::entityManager()->getBundleInfo($entity_type)) as $bundle) {
-    $bundle_instances = array_filter(\Drupal::entityManager()->getFieldDefinitions($entity_type, $bundle), function ($field_definition) {
-      return $field_definition instanceof FieldInstanceConfigInterface;
-    });
-    if (isset($bundle_instances[$field_name])) {
-      $instance = $bundle_instances[$field_name];
-      $label = $instance->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.
-  arsort($label_counter);
-  $label_counter = array_keys($label_counter);
-  return array($label_counter[0], $all_labels);
-}
-

@@ -182,10 +153,32 @@ function field_views_field_default_views_data(FieldConfigInterface $field) {
-  list($label, $all_labels) = field_views_field_label($entity_type_id, $field_name);
+  $label_counter = array();
+  $all_labels = array();
+  // Count the amount of instances per label per field.
+  $instances = field_info_instances($entity_type_id);
+  foreach ($instances as $bundle => $bundle_instances) {
+    if (isset($bundle_instances[$field_name])) {
+      $instance = $bundle_instances[$field_name];
+      $label = $instance->getLabel();
+      $label_counter[$label] = isset($label_counter[$label]) ? ++$label_counter[$label] : 1;
+      $all_labels[$label] = TRUE;
+    }
+  }
+
+  if (empty($label_counter)) {
+    $label = $field_name;
+  }
+  else {
+    // Sort the field labels by it most used label and return the most used one.
+    arsort($label_counter);
+    $label_counter = array_keys($label_counter);
+    $label = $label_counter[0];
+  }

Pardon, but I don't see why not having all that logic in a separate function is not a good idea in the first place?

xjm’s picture

Issue tags: -Novice

Removing the Novice tag until there is consensus.

Mile23’s picture

Status: Needs review » Closed (won't fix)

field_views_field_label() was changed to views_entity_field_label().

Change record: https://www.drupal.org/node/2402215