Last updated March 10, 2012. Created by tim.plunkett on November 27, 2011.
Edited by dale42, wmostrey, pfrenssen, kalman.hosszu. Log in to edit this page.
Overview
The Field Collection module is the successor to the CCK3 Multigroup. It allows a set of fields to be combined together and treated as a single field. For example, a field collection named Playlist can be created consisting of the text fields Song and Artist. When a Playlist entry is created the Song and Artist fields are presented together as elements of the Playlist field.
Usage
When the Field Collection module is installed a new Field type named 'field collection' becomes available in the Manage fields tab of supported entities. In a basic Drupal installation these are:
- Content types
e.g. Home > Administration > Structure > Content types > Basic page > Manage fields - People/Accounts
Home > Administration > Configuration > People > Account settings > Manage fields - Taxonomy
e.g. Home > Administration > Structure > Taxonomy > Tags > Manage fields
After the field collection field is created it's managed under from Home > Administration > Structure > Field collections. This is where fields are added to the field collections created in the Manage fields tab.
There is one permission, Administer field collections. It allows the holder to create and delete fields on field collections.
Example Usage
Make sure both the Field Collection and Field UI modules are enabled to follow this tutorial.
You are creating music playlists, which consists of multiple songs. Each song has a specific song name and artist.
- Go to admin/structure/types/add. Create a new content type: Playlist
- Add a new field to it: Track
- type: Field collection
- widget: Embedded
- number of values: Unlimited)
- Go to admin/structure/field-collections and click on "Manage fields" for the created Track field
- Add two fields to it: Song, Artist (type Text)
When you now add a new node of type "Playlist" you can add as many tracks to it as you like.
Comments
What about loading field collections for nodes?
Node load doesn't seem to load the field collection of hosting node.. The previous examples show how to save values to a node, what about loading existing values?
Thanks
The field_collection entity
The field_collection entity id(s) are available to you when using node_load via $node->field_collection_name. Once you've gotten the entity_id you can use entity_load('field_collection', $node->field_collection_name[LANGUAGE_NONE][0]['value']) to get the entire entity as an object. Something like this will get you all of the data:
<?phpforeach ($node->field_collection_name[LANGUAGE_NONE] as $entity_id) {
$entity = entity_load('field_collection', $entity_id['value']);
dpm($entity);
}
?>
this worked for me in
this worked for me in D7
<?php
entity_load('field_collection_item', $coll_item_id);
// in my case the $coll_item_id comes from $node->MY_FIELD[LANGUAGE_NONE]['0']['value']
?>
My bad, my original code
My bad, my original code should have been 'field_collection_item', not 'field_collection'.
Pass array of ids to entity_load()
Also, as per the signature of entity_load(), the example code should be
<?phpforeach ($node->field_collection_name[LANGUAGE_NONE] as $entity_id) {
$entity = entity_load('field_collection_items', array($entity_id['value']));
dpm($entity);
}
?>
I believe
I believe
entity_load('field_collection_items'should beentity_load('field_collection_item'without the "s".Correct :) So
Correct :)
So finally:
<?phpforeach ($node->field_collection_name[LANGUAGE_NONE] as $entity_id) {
$entity = entity_load('field_collection_item', array($entity_id['value']));
dpm($entity);
}
?>
Can there be some
Can there be some documentation on theming field collections? I don't know where to put field-collection-item.tpl.php and in what name. I found couple of threads talking about it, but information was not quite concrete, and when I do what is mentioned in them, it is not working.
The method of Mareks works
The method of Mareks works, althought without identifiers for me. I used
<?php$test = entity_load('field_collection_item', $ids = FALSE);
echo '<pre>';
var_dump($test);
?>
$test now becomes an array with everything associated witht the field_collection_item. If you have more collection, they will all be in there. Now it's a matter of extracting what you need from it. Will update this comment if I know more.
Edit: this next bit gets a value from the collection, thanks to sijuwi who just posted a ticket to this thread http://drupal.org/node/1429842!
<?php$field_collection = entity_load('field_collection_item', array($node->field_lima_variant_coll['und'][0]['value']));
print $field_collection[1]->field_lima_variant_text['und'][0]['value']
?>
More documentation on using this programatically please?
I tried to do some more advanced use of this module and as of 3/2012 it's not fun. Some things I could use help understanding:
What happens when a node is cloned?
// for instance...$node = node_load($my_old_id);
$node->nid = NULL;
node_save($node); //cloning the old node
Do the field collection items pointing at the old node now point at BOTH nodes? Or are they cloned too?
And how to delete/remove field collection associations with a node? At the moment there's plenty of documentation about how to add field collections, and collect their data - what about deleting one or more field collection values from a node? Or changing them to point to a different field collection value, etc?
Thanks very much.
how to create a field collection while content type creation pro
I want to create a field collection to group two fields in a content type programmatically so that when the user installs the module, the module installs the content type and let the user create content through 'Add Content' option .
I have created two fields and field collection.I have created the field using field_create_field function in HOOK_install. I dont know how to link the field collection with the two fields in the code. I have searched over forum and I came across various materials just explaining how to add,edit and delete values of field collection programmactically but I could not find any useful information on how to create field collection in content type. Any help on this topic will be much appreciated.
In my code to create the content type with the field collection,the content type is created well with the field collection but when I check in the structures -> field Collection -> field_group->manage field, The fields are not getting displayed there and when I go to 'add content' the field collection does not have those fields but the fields are displayed outside the field collection container