7.x field.info.inc field_info_instances($entity_type = NULL, $bundle_name = NULL)

Retrieves information about field instances.

Use of this function to retrieve instances across separate bundles (i.e. when the $bundle parameter is NULL) should be avoided when possible, since it loads and statically caches a potentially large array of information. Use field_info_field_map() instead.

When retrieving the instances of a specific bundle (i.e. when both $entity_type and $bundle_name are provided), the function also populates a static cache with the corresponding field definitions, allowing fast retrieval of field_info_field() later in the request.

Parameters

$entity_type: (optional) The entity type for which to return instances.

$bundle_name: (optional) The bundle name for which to return instances. If $entity_type is NULL, the $bundle_name parameter is ignored.

Return value

If $entity_type is not set, return all instances keyed by entity type and bundle name. If $entity_type is set, return all instances for that entity type, keyed by bundle name. If $entity_type and $bundle_name are set, return all instances for that bundle.

See also

field_info_field_map()

Related topics

10 calls to field_info_instances()
CommentFieldsTest::testCommentDefaultFields in modules/comment/comment.test
Tests that the default 'comment_body' field is correctly added.
entity_form_submit_build_entity in includes/common.inc
Copies submitted values to entity properties for simple entity forms.
FieldAttachStorageTestCase::testFieldAttachLoadMultiple in modules/field/tests/field.test
Test the 'multiple' load feature.
FieldInfoTestCase::testFieldInfo in modules/field/tests/field.test
Test that field types and field definitions are correcly cached.
FieldTranslationsTestCase::testFieldDisplayLanguage in modules/field/tests/field.test
Tests display language logic for translatable fields.

... See full list

File

modules/field/field.info.inc, line 596
Field Info API, providing information about available fields and field types.

Code

function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
  $cache = _field_info_field_cache();
  if (!isset($entity_type)) {
    return $cache
      ->getInstances();
  }
  if (!isset($bundle_name)) {
    return $cache
      ->getInstances($entity_type);
  }
  return $cache
    ->getBundleInstances($entity_type, $bundle_name);
}

Comments

donquixote’s picture

This lovely function returns everything except for the field type.
Duh.

bcweaver’s picture

Like++

loopduplicate’s picture

$fields_info = field_info_instances($entity_type, $bundle_name);
foreach ($fields_info as $field_name => $value) {
  $field_info = field_info_field($field_name);
  $type = $field_info['type'];
  echo "The type is $type, duh.";
}
jmonkfish’s picture

I like what you did there. Thanks. :)

luxpaparazzi’s picture

did you find any solution to this problem?

schlicki’s picture

Are the fields returned by field_info_instances ordered in any way?
I am creating a PDF from nodes and want to let the user select a display or the edit form order of the fields. For performance reasons I could just use the returned array as is and only order it by myself when a display is selected - in case the array returns the fields ordered as in the widget..

Thanks

sepehr.sadatifar’s picture

Did you find how it's ordered?

SteffenR’s picture

I also had the problem of unordered fields while calling field_info_instances function.
To solve this issue i added an array_multisort in my custom module - hope it also helps to solves your ordering issue.

// Get all fields of entity type.
$page_fields = field_info_instances('node', 'page');
// Sort fields by it's display order.
$field_weight = array();
foreach ($page_fields as $key => $value) {
  $field_weight[$key] = !empty($value['display']['default']['weight']) ? $value['display']['default']['weight'] : 0;
}
array_multisort($field_weight, SORT_ASC, $page_fields);

SteffenR

mlncn’s picture

/**
 * Helper function to return all fields of one type on one bundle.
 */
function fields_by_type_by_bundle($entity_type, $bundle, $type) {
  $chosen_fields = array();
  $fields = field_info_field_map();
  foreach ($fields as $field => $info) {
    if ($info['type'] == $type &&
        in_array($entity_type, array_keys($info['bundles'])) &&
        in_array($bundle, $info['bundles'][$entity_type]))
    {
      $chosen_fields[$field] = $field;
    }
  }
  return $chosen_fields;
}

And use it like so, to get all taxonomy fields on the article content type:

  $fields = fields_by_type_by_bundle('node', 'article', 'taxonomy_term_reference');
aaronbauman’s picture

liquidcms’s picture

This is not the D8 replacement as it requires entity type and bundle to be passed. I believe D8 no longer has an equivalent.