7.x field.info.inc field_info_instance($entity_type, $field_name, $bundle_name)

Returns an array of instance data for a specific field and bundle.

The function populates a static cache with all fields and instances used in the bundle, allowing fast retrieval of field_info_field() or field_info_instance() later in the request.

Parameters

$entity_type: The entity type for the instance.

$field_name: The field name for the instance.

$bundle_name: The bundle name for the instance.

Return value

An associative array of instance data for the specific field and bundle; NULL if the instance does not exist.

Related topics

41 calls to field_info_instance()
CommentTokenReplaceTestCase::testCommentTokenReplacement in modules/comment/comment.test
Creates a comment, then tests the tokens generated from it.
DisabledNodeTypeTestCase::testDisabledNodeTypeUpgrade in modules/simpletest/tests/upgrade/upgrade.node.test
Tests a successful upgrade.
FieldAttachStorageTestCase::testFieldAttachCreateRenameBundle in modules/field/tests/field.test
Test field_attach_create_bundle() and field_attach_rename_bundle().
FieldAttachStorageTestCase::testFieldStorageDetailsAlter in modules/field/tests/field.test
Test storage details alteration.
FieldBulkDeleteTestCase::testDeleteFieldInstance in modules/field/tests/field.test
Verify that deleting an instance leaves the field data items in the database and that the appropriate Field API functions can operate on the deleted data and instance.

... See full list

File

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

Code

function field_info_instance($entity_type, $field_name, $bundle_name) {
  $cache = _field_info_field_cache();
  $info = $cache
    ->getBundleInstances($entity_type, $bundle_name);
  if (isset($info[$field_name])) {
    return $info[$field_name];
  }
}

Comments

hkirsman’s picture

$field_instance = field_info_instance('node', 'field_body', 'article');

capellic’s picture

The code for accessing a field on a field collection that's attached to a node:

$field_instance = field_info_instance('field_collection_item', $field_name, $field_collection_name);

SKAUGHT’s picture

+1

texas-bronius’s picture

User object should specify 'user' as its entity type and bundle, as in:

if (field_info_instance('user', 'field_subscription_sid', 'user')) {
  // The custom field exists on the user object
}

See https://www.drupal.org/node/1261744 under Bundles.
The above check might be useful if you need to confirm that a Feature Export has reverted before assuming use of a field provided by it.

sobi3ch’s picture

@texas-bronius +1 for example!

wouters_f’s picture

$fid = 'field_promote';
$voc_name = 'tags';
$inst_info = field_info_instance('taxonomy_term', $fid, $voc_name);
soyarma’s picture

To load the field instance info you need to do the following (from: https://www.drupal.org/node/2260037).

use Drupal\field\Entity\FieldConfig;
class MyClass {

  // $entity_type, $bundle, $field.
  $field_info = FieldConfig::loadByName('node', 'page', 'myfield');

}
kkalaskar’s picture

//$instance = field_info_instance(ENTITY_TYPE, FIELD_NAME, BUNDLE);
$city_instance = field_info_instance('profile2', 'FIELD_NAME', 'PROFILE_TYPE');
$city_instance = field_info_instance('profile2', 'field_provider_address', 'provider');

Prashant.c’s picture

To get the information about fields used in comments

$entity_type = 'comment';
$field_name = 'field_file_upload';
$bundle_name = 'comment_node_' . content_type_machine_name '; // comment_node_page
field_info_instance($entity_type, $field_name, $bundle_name);

// To get the $bundle name you can use following code.
$info = _field_info_collate_fields();
var_dump($info['instances']['comment']);
firewaller’s picture

Thanks! This was really bugging me.

Tharick’s picture

how to get translated fields label?

joevanni99’s picture

This is the best solution I found.
You can retrieve it like this (assuming you have an entity called 'entity_type' with a field called 'field_name' and a bundle called 'bundle':

  $field = field_info_instance('entity_type', 'field_name', 'bundle');
  $t = get_t();
  $translated_label = $t($field['label']);