7.x field.module field_get_items($entity_type, $entity, $field_name, $langcode = NULL)

Returns the field items in the language they currently would be displayed.

Parameters

$entity_type: The type of $entity; e.g., 'node' or 'user'.

$entity: The entity containing the data to be displayed.

$field_name: The field to be displayed.

$langcode: (optional) The language code $entity->{$field_name} has to be displayed in. Defaults to the current language.

Return value

An array of field items keyed by delta if available, FALSE otherwise.

Related topics

6 calls to field_get_items()
comment_admin_overview in modules/comment/comment.admin.inc
Form builder for the comment overview administration form.
comment_preview in modules/comment/comment.module
Generate a comment preview.
comment_tokens in modules/comment/comment.tokens.inc
Implements hook_tokens().
file_file_download in modules/file/file.module
Implements hook_file_download().
node_tokens in modules/node/node.tokens.inc
Implements hook_tokens().

... See full list

File

modules/field/field.module, line 940
Attach custom data fields to Drupal entities.

Code

function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
  $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}

Comments

jacobson’s picture

It is worth noting that field_get_items() returns raw values the use of which can be a security risk. Passing the raw values through field_view_value() handles the sanitizing of the values.

plach’s picture

field_get_items() without an explicit langcode parameter should be used only in a display/render context since it will return the field items corresponding to the current display language.

a.milkovsky’s picture

Is there any function like "field_set_items()" ? To set value of field?

raccer’s picture

tea.time’s picture

This might be obvious, but the first time I tried this function to pull a field from a single taxonomy term, I passed "term" as $entity_type. No-go -- got a WSOD with an unhelpful error. As taxonomy_entity_info() shows, it's "taxonomy_term".

daggar’s picture

I was hunting around for just this information.

Is there a list of core entity types (and their machine names) that I've overlooked? If there isn't, I might compile one. It's basic information that should be available in one spot, rather than having to hunt around individual API calls to find.

zhangx1a0’s picture

Exactly what I am looking for...

woop_light’s picture

a practical example would be useful.

i have a custom teaser "node--project--teaser.tpl.php" (used some php to enable this teaser override). i want to pull the raw value of the "style" field (field_style). how do i use field_get_items to do this? please don't get wrapped up in whether i'm trying to pull the first value or all of the values. either is fine.

javment’s picture

Hi,
Is there a way to get for example the first item of the field list.What I mean, is if for example in a node type I let the user to add 1 to 10 image files fields, when I list the nodes in teaser mode I only want to get the first image to display to the user and not to load all the image data.

Thank you!

Jenechka’s picture

  $my_field_items = field_get_items('node', $node, 'field_my_field');
  if ($my_field_items) {
    $my_field_first_item = reset($my_field_items);
    $my_field_value = $my_field_first_item['value'];
  }
arnoldbird’s picture

"An array of field items keyed by delta if available, FALSE otherwise."

What does "if available" mean? Set? Set and not empty? Does it vary depending on the field type?

Is a property assigned to an entity anytime the field exists, or only when the field is set with a non-empty value?

sheldonkreger’s picture

You can always check your entity type by using entity_get_info()
http://api.drupal.org/api/drupal/includes!common.inc/function/entity_get...

kevla’s picture

Does anyone know of an equivalent function to return the value of a field in cck (without having to know the structure of the data in advance?)

zoo33’s picture

Make sure you have a valid entity object before calling this function.

field_get_items('node', NULL, 'seeya');

…will cause a fatal EntityMalformedException without any real hints on where the error originated.

jvsteiner’s picture

This helped send me in the right direction. I was getting EntityMalformedException errors with:

$image_count = count(field_get_items('node', $node, 'field_image'));

but it works with:
$image_count = count(field_get_items('node', $entity, 'field_image'));

texas-bronius’s picture

The function field_get_items() works against any valid node structure, so in a hook_update_node or hook_presave_node, you can pass in $node->original where the examples above specify simply $node as in:

$node_before = $node->original;
$field_items = field_get_items('node', $node_before, 'field_some_field');
codethenode’s picture

I'm experiencing a weird situation where field_get_items() works for anonymous users, or developer roles, but not administrator roles. I'm using a subtheme of the D7 Aurora theme. Anyone seen anything like that before?

aaronbauman’s picture

This is a relatively dumb pass-through function.
There's no access control layer here to explain the symptom you're describing.
You're probably experiencing an unrelated issue.

jlnd’s picture

I had a hard time figuring this out, so in case it's useful to anyone else... When using field_get_items to get fields that belong to paragraph bundle items, I used:

field_get_items('paragraphs_item', $variables['paragraphs_item'], 'field_FIELDNAME');

annared’s picture

Anyone knows how to use this inside a panel?
Thanks