Last updated November 28, 2012. Created by dale42 on March 11, 2012.
Edited by zarrex. Log in to edit this page.
When a field collection is added to a user, node, or other Drupal entity it is represented with a field type of field_collection_item. The value stored in the field_collection_item field is the id of the field collection, not the individual values of the fields making up the field_collection_item.
For example, take the following configuration:
- Node type Playlist
- Playlist has a field collection named Track (field_track)
field_track of type field collection - Track field collection consists of the fields Song and Artist
field_song of type text
field_artist of type text
A Playlist node is created with two tracks. For the purposes of the example the following values are assigned:
- Playlist node id: 25
- 1st track: field_collection id: 17
field_song: The Drupal Song
field_artist: Lullabots - 2nd track: field_collection id: 18
field_song: Drupal Heaven (I Shall Be Released)
field_artist: The Secret Kitten Killers
When node 25 is loaded, field_track has the following value:
[field_track] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 17
)
[1] => Array
(
[value] => 18
)
)
)To get the values of the individual field items call the entity_load function:
<?php
$node25 = node_load(25);
$track_1 = entity_load('field_collection_item', array($node25->field_track[LANGUAGE_NONE][0]['value']));
$track_2 = entity_load('field_collection_item', array($node25->field_track[LANGUAGE_NONE][1]['value']));
?>
Comments
According to the function
According to the function signature. The second parameter of entity_load(..., array($id)); needs to be an array of IDs. There is no check if just a numerical value is given. So, wrap a single argument in array() to make this example work.
Error
The error is Fatal error: Unsupported operand types in /home/tim/Projects/fba/htdocs/includes/entity.inc on line 354
and yes best to put in array
$comp = entity_load('field_collection_item',array(1));
Great work by the way!
better way to render field collection items
here comes working version to render field collection fields one by one. here "field_references" is my field collection name which has two fields, named as "field_reference_title" and "field_reference_location".
Below is code snippet for how to print field collection in node.tpl file.
<?php
$wrapper = entity_metadata_wrapper('node', $node);
$formtype = field_get_items('node', $node, 'field_references');
foreach($formtype as $itemid) {
$item = field_collection_field_get_entity($itemid);
print $item->field_reference_title['und'][0]['safe_value'];
print $item->field_reference_location['und'][0]['safe_value'];
}
?>
Enjoy!!!!! :)
is it possible to have an
is it possible to have an alternative to:
$item->field_reference_title['und'][0]['safe_value'];
without having to specify the ['und'] language?
You have to iterate through
You have to iterate through the langcodes then. Btw. there is a global Constant: LANGUAGE_NONE.
$item =
$item = field_collection_field_get_entity($itemid);
field_collection_field_get_entity doesn't exist in the API.
Web application developer
http://kmi.open.ac.uk/
how to
I wanted to get all the terms in field collection fields,attached to profile2 of an user the following code is throwing Entity malfunction exception error can some one help me please
<?php$user_fields = field_get_items('field_collection_item', $account, $field_name);
if (!empty($user_fields)) {
foreach ($user_fields as $term) {
$user_terms[$field_name][$term['tid']] = TRUE;
?>
How I can create Field Collection by Services?
Create by AJAX post like it in http://drupal.org/node/1354202
{"field_collection":[
{
"first_field":"100",
"secondfield":"20"
}
]
}