Power Site Building With Field Collection

In Drupal 6 fieldsets allowed site builders to group form items together. If a client asked for the ability to attach extra supporting data to a content type, you were stuck creating another content type and then create a relationship between both types using a node reference field. In Drupal 7 you would simply create a field collection for the supporting data.

When you create a field collection field, you are in fact creating an entity which will be attached to the host entity when a piece of content is created. Because field collections are entities, site builders can attach any type of field to a collection. In this article, I'll demonstrate how to use the Field collection module by creating a content type with a few fields. Field collection requires the Entity API module. Before you download Field collection make sure Entity API in downloaded and installed.

Field collection does not really have any type of configuration, you simply install the module and start using it.

In this example, I have a content type called "Story". I want to give users the ability of adding any number of authors per story. When users add an author to a story they'll be able to enter in a first name, last name and profile image.

Below is what the collection will look like:

Go to Structure > Content types, and click on manage fields.

Create a field called "Authors" and select "Field collection" as type and "Embedded" as the widget.

Once you're on the edit page select "Unlimited" as the "Number of values".

All we have done so far is created a Field collection field. Next, we'll have to attach fields to the author collection. Go to the Structure page and click on Field collections.

On the Field collections page, we can see the author collection. Click on manage field and attach fields the same way as on content types. In this example, I'll add the first name, last name and profile image field.

To see the fields in action go ahead and create a story.

You can also add/edit/delete individual field collection entities. On the node page you should see an edit, delete and add link.

The last thing I want to explain is how to make a field within a collection mandatory. If we want to make the first and last name mandatory in our example, we have to check the "Required field" checkbox on both fields (first and last name), as well as the field collection field (authors) itself. If you just make first and last name fields mandatory, the form wont validate. You have to make sure the field collection field is set as mandatory.

As you can see, the Field collection module is very powerful. If you have any questions, please leave a comment.

4 thoughts on “Power Site Building With Field Collection”

  1. Hey, love the tutorials, was wondering if you’ve played with this in views. About to take the plunge. Was wondering where/how you filter down a fields_collection at. Inside the filters of views, or must you handle it before hand.

    E.g. on one view you just want the authors last names, on another you want both. However both these fields are contained inside a field collection. I know this is outside the scope of your tutorial, but thought I’d ask. Thanks!

  2. Is there any recourse for data that was created prior to installing Field Collections that should be collected? As far as I can tell, existing data would need to be reentered in order to be part of a collection, but I’m hoping that’s not actually the case…


Comments are closed.

Scroll to Top