Last updated March 16, 2014. Created on November 27, 2011.
Edited by Lenn-art, dale42, wmostrey, pfrenssen. 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

Manage or adding a field collections

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.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

kharbat’s picture

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

csdco’s picture

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:

<?php
foreach ($node->field_collection_name[LANGUAGE_NONE] as $entity_id) {
 
$entity = entity_load('field_collection', $entity_id['value']);
 
dpm($entity);
}
?>
mareks’s picture

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']
?>
csdco’s picture

My bad, my original code should have been 'field_collection_item', not 'field_collection'.

girishmuraly’s picture

Also, as per the signature of entity_load(), the example code should be

<?php
foreach ($node->field_collection_name[LANGUAGE_NONE] as $entity_id) {
 
$entity = entity_load('field_collection_items', array($entity_id['value']));
 
dpm($entity);
}
?>
csdco’s picture

I believe entity_load('field_collection_items' should be entity_load('field_collection_item' without the "s".

girishmuraly’s picture

Correct :)

So finally:

<?php
foreach ($node->field_collection_name[LANGUAGE_NONE] as $entity_id) {
 
$entity = entity_load('field_collection_item', array($entity_id['value']));
 
dpm($entity);
}
?>
orgnsm’s picture

is there a way to obtain the values other than using entity_load or entityFieldQuery? i'm looking to use db_select or db_query for speed and flexibility...

mghatiya’s picture

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.

Nehbur’s picture

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']
?>
hermes_costell’s picture

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.

Drupal dev’s picture

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

shocknawe’s picture

Hi,

I'm currently trying to use this with the 'View Datasource' to create a 2D array of objects in xml.
http://drupal.org/project/views_datasource

I'm not yet sure on how to do it properly, so i have to ask...
Is this possible?

Time is of the essence

shomari’s picture

Did you have any success using this module with View Datasource?

pmichelazzo’s picture

I'm thinking about the differences between field collection and content type in a scenario like this:

I have a recipe with different steps to do it. What's the best: use a field collection where I can put the image and description of the step or use a content type "Steps recipes" with the image and description fields inside and make a entity reference for the recipe content type?

I think that this scenario could be anyone, e.g. artists and their songs, malls with their stores, etc.

So, when we use a field collection or a content type?

Best

Paulino Michelazzo
Web & CMS Consultant
http://www.michelazzo.com.br

mindxplorer’s picture

I would like to use the particular values of the fields grouped in a field collection for rewriting the output in a view. But I struggle to find the right token to achieve this. In the token list (under the rewrite field) only the whole fields collection is mentioned.
Is it possible to access the fields of a field collection directly?

BrendanSmith’s picture

Didn't see this mentioned anywhere, apologies if it has. For those who want to know, you can add new view modes for your field collections programmatically much like you would for nodes with a hook_entity_info_alter().

For Example

function MY_MODULE_entity_info_alter(&entity_info) {
$entity_info['field_collection_item']['view modes']['test'] = array(
    'label' => t('test'),
    'custom settings' => TRUE,
  );
}