Field collections

The Field Collection module is meant to deal with an issue that has been referred to by multiple names, including “repeating field groups”. For example, if you want to store data for a given name, a last name, and a role (e.g. “author”, “illustrator”, “editor”, etc.), and allow there to be multiple values (in order to accommodate the different people and roles involved in book development), you’ll need to use a field collection.

The field group module (a different module altogether) allows you to group fields together, but only for display purposes. The fields have no durable connection to one another. If you want to pull up, for instance, the last name of every illustrator using the Views module, it isn’t possible if you’re just using field group. Field group also allows any of the fields within it to have multiple values (e.g. you can add any number of given or last names), but it doesn’t have a way to say “the first given name and the first last name listed in this field group belong to a single person”. The way to create groups where the contents behave (roughly) as a unified entity, and there can be multiple instantiations of the group, is to use Field Collection.

Using field collections

Creating and populating a field collection is a multi-step process. After you’ve installed the module, you’ll be able to add a new field type, “Field collection”, to any content type, user profile, or anywhere else you can add fields. The default “embedded” widget is fine. Adding a field collection field just creates the “container” for the fields that will repeat. To actually add in the fields you want in your field collection, you have to save the field collection field, then go to Structure > Field collections > [your-field-collection-name]. This will give you an interface that looks just like the interface for managing fields for a content type, and you can add the fields that should be available as part of the field collection.

Once you have added some fields, you can also go to Structure > Field collections > [your-field-collection-name] > Manage display to manage how the fields within the field collection will display. Keep in mind that you have to go to the manage display settings for the field collection to do this -- rather than the content type that contains the field collection. The manage display page for the content type will have options for how to display the field collection as a whole (e.g. whether or not to include edit links for individual sets of items, what view mode to use to display the field collections), it won’t have the options for how to display each field within the field collection.

Field collections and Views

Using field collections in Views is a little tricky. Let’s say you have a Person content type, and a field collection field within it to list all of the person’s publication titles (a plain-text field called “Book title”), publication dates, and their role (author, editor, illustrator, etc.) in the publication. Let’s say you have a grid display of people (as described in chapter 12 for the example site), and you want to list below their name the titles of all the books they were involved with. You may be tempted to just add the “Book title” field to the view, but that won’t display any data. Data stored in a field collection is actually stored outside the node where the field collection appears-- almost as if the data were stored in a different node that’s connected via a node reference field (c.f. section 13.4 for an example of using Views to pull in data from a connected Person node when you’re displaying Events.)

To display data stored in a field collection, you need to open the “Advanced” column in Views (on the far right), and under “Relationships”, add a contextual filter corresponding to the name of the field collection field itself. So, if in our example the field collection field itself were entitled “Publications”, that’s what you would add under “Relationships”. The default settings should be fine.

Then, under “Fields” in the far left, you can add the “Book title” field and choose “Publications” as the relationship, and your data should show up.