Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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:
$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.
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/
problem with imagen
It work for me but if the field it's an imagen the imagen don't show, any idea?
better way to render field collection items
helped me.thank you.
It Works!
It Works for me. Thank you lot :)
Getting error
Thank you for this great example. However I'm getting an error with the above code I can't trace. My entity metadata wrapper works fine, but the reference to the field collection is throwing an error that's not really telling me anything helpful.
Here's my code:
This gives error:
EntityMalformedException: Missing bundle property on entity of type entityform. in entity_extract_ids() (line 7922 of C:\wamp\www\mysite\includes\common.inc).
Not sure what's causing the error. Also, in your example, is the wrapper line necessary? It doesn't seem to be referenced in the rest of the code.
* _wrapper_debug code from EMW info page
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
How I can create Field Collection by Services?
Create by AJAX post like it in http://drupal.org/node/1354202
Duplicating a node and it's field collection items
Hi,
I have a node that has field_collections. I would like to duplicate this node and it's field collection items. If I just did a straight duplication of the node by removing the nid value and saving the node object. this will probably replicate the field collection items however, i need to generate new field collections identical in data but, unique as field collection items go.
Is there a proper way of doing this?
Thanks.
Best way to update a collection field
I try to update a collection field, but can't save the field.
I need to update the field when the user save the node so I use hook_node_update
I try with a second method :
www.almedestudio.com
final solution
Hi Zorax!,you could found a solution to this?, sorry for comment after a long time but i have similar code like you. I hope you could help me. Thank you!.