There does not appear to be a field_info function for retrieving all the instances of a given field. Or am I missing it?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

True. field_info_instances() takes a $bundle_name, so we would need to find a different name - field_info_instances_of_field($field_name) or something

The information of instances of a $field is not compiled by _field_info_collate_fields() (instances are keyed by bundle name first).

1) we have _field_info_collate_fields() gather both informations and field_info_instances_of_field($field_name) can read from $info['instances_by_field']

  foreach ($instances as $instance) {
    $info['instances'][$instance['bundle']][$instance['field_name']] = $instance;
+  $info['instances_by_field'][$instance['field_name']][$instance['bundle']] = $instance;
  }

2) we have field_info_instances_of_field($field_name) build from the existing $info['instances'] and loop on bundles to find the right $field_name.

1) doubles the size of the cached information, 2) adds (probably minimal) CPU cost on each call
I guess I'd go with 2)

bjaspan’s picture

Status: Active » Needs review
FileSize
1.53 KB

How about this? _collate_fields stores an extra element in each field structure, 'bundles', an array of the bundle names for which instances of the field exist. Takes minimal storage space and no extra cpu. If all you want is a count of instances per field, a count of bundles is the same. If you actually want the instances, it is easy to iterate over field_info_instance().

Status: Needs review » Needs work

The last submitted patch failed testing.

bjaspan’s picture

Status: Needs work » Needs review
FileSize
3.58 KB

There is NO WAY this patch caused a test failure, and the tests all pass after cvs update on my system, so I have no idea why the 1 test failed for the last patch.

New patch is the same as the old one with improved phpdoc.

yched’s picture

Status: Needs review » Reviewed & tested by the community

+1, and tests work for me too.

bjaspan’s picture

Re-rolled and removed double-spaces after periods.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Sorry! Thought I'd committed this already.

Done. :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.