Opens in a new tab

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 comments

Comments are closed.

✦ WebWash Premium

Go deeper with Premium

Lifetime access to every premium course, the private member forum and member-only live streams.

16+ in-depth courses
Private member forum
Member-only live streams
Ad-free, forever
Get lifetime access · €99

One-time payment · plus VAT