function field_get_items

You are here

7 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

5 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().

File

modules/field/field.module, line 935
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

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.

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.

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

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".

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.

Exactly what I am looking for...

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.

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!

<?php
  $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'];
  }
?>

"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?

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

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?)

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.