| 7 field.module | field_get_items( |
| 8 field.module | field_get_items(EntityInterface $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
Related API Function field_view_value()
PermalinkIt 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.
Context
Permalinkfield_get_items() without an explicit
langcodeparameter should be used only in a display/render context since it will return the field items corresponding to the current display language.set value of field
PermalinkIs there any function like "field_set_items()" ? To set value of field?
on set & get
PermalinkSee here for info on the entity API (being able to set & get)
http://drupal.stackexchange.com/questions/24699/whats-best-practice-when-working-with-the-languageund
$entity_type for taxonomy terms is "taxonomy_term"
PermalinkThis 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".
Thank you.
PermalinkI 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.
Thank you!!!
PermalinkExactly what I am looking for...
how would i use this?
Permalinka 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.
Here is a practical example (theming)
Permalinkhttp://highrockmedia.com/blog/drupal-7-fields-digging-deep-data-themers
Get only one item
PermalinkHi,
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!
Get first item only
Permalink<?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'];
}
?>
what is "available"?
Permalink"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?
Discover entity type
PermalinkYou can always check your entity type by using entity_get_info()
http://api.drupal.org/api/drupal/includes!common.inc/function/entity_get...