Drupal 7 Field Collection Module

By shane
Mon, 2012-12-03 22:36
Daily Dose of Drupal Episode #59

Share with Others

The Drupal 7 Field Collection module allows fields to be grouped together and then dynamically added to another content type. In this example, we go through how to use the Drupal 7 Field Collection module to add songs (with song title, and song artist fields) to a Drupal content type called Playlist. This example is based on the documentation of the Drupal 7 Field Collection module.

In this episode you will learn:

  • How the Drupal Field Collection module can be used to group collections of fields together to be added to another content type
  • How the Drupal Field Collection module can be used as an embedded form within a node edit page
  • A few real use case examples of where this module can be useful

Hello everyone and welcome to another Daily Dose of Drupal. As always I’m Shane and you can follow me on Twitter @smthomas3, you can also sign up for the codekarate.com newsletter.

Today we’re going to be talking about the Field Collection Module, it’s a module I learned about fairly recently but it is extremely useful and it’s probably one of my favorite modules in all honesty. It’s not something we’re going to use every day but we’re going to go ahead and get started.

I already have it downloaded and installed on my Drupal 7 site so you’ll notice the Field Collection Module has been installed, you set permissions forward of course, we’re going to go ahead and configure it.

It says here that no Field Collections that have been defined yet so we’re going to go ahead and show you what this can do. An example of this is the examples that you used in the documentation is to add a content type called Playlist and it add a bunch of songs with, you know, maybe the artists along with it and so you’re going to have a grouping of fields so you can add multiple items to one content type.

So we’re going to go ahead and go through that example, we’re going to call it Playlist and keep everything else the same and for Playlist we’re going to go Manage Field. So the title is going to be the Playlist name, the body can be the description of the playlist but we’re going to add a Field Collection field and we’re going to call this Songs, you can either select the hidden widget or the embedded, I’m going to go ahead and select the embedded, I’m going to click Save.

Now it says the song has no field settings, that’s okay we’ll go ahead and let’s say we can set everything else, looks good so we’ll save that. Now at the Field Collection so we’ll need to come back into the Field Collection Configuration Settings, we’ll go to configure and now you can see our Fields shows up here.

So now as you can see you can manage fields within this field so a song may have a title of the song, we’ll go ahead and call it Song Title and we’ll call that a Text Field, keep everything else the same, add it’s defaults and we’ll also go Song Artist, we’ll also make that a Text Field but you could of course use any types of fields, in fact you could use a Field Collection within a Field Collection but in this case we should keep two text fields, we’ll save that and now if we come back to our original content type of playlist you’ll see everything looks the same, we can come in to the display and it shows that it’s going to show you the field collection items, you could change this format, we’re going to keep it pretty basic, you can of course change it and see how that changes things, we’ll go ahead and add a playlist, I’ll call this My Songs and you can see down here it has a Song Title and Song Artist, it’s only showing one here.

That’s because I forgot to allow multiple items, let’s go back in here to the content type into Playlist and we’ll go into Manage Fields, go back to songs and we’re going to allow number values to be unlimited.

You could change this to more than one but we’re going to leave this unlimited so you can dynamically add as many as you need, now we’re going to go back to that and add a playlist, my songs and now you can see we have a title, Song Title 1, Song Artist 1, you can add another item, Title 2, Artist 2 and then obviously you can put real songs in here but the cool thing is you can of course rearrange it, change it up, once you save it and this is what the embedded widget allows you to do is to change that within the Edit Screen there and once you’re on the View, this is the Node View of this playlist, you could also edit right from here so you can edit this particular song, so I can change the title, save that, you’ll notice that it’s now represented, I can now delete specific items, I can also add new one and now that will drop in the bottom and as you can see it allows you to build a collection of fields together and use that to expand a single content type.

So the content type is Playlist but the playlist may have multiple songs. If this was just a list of song titles you wouldn’t need Field Collection Module, you could just use a Text Field and allow unlimited options for that but since we need … since a song is a grouping of title and artists, the Field Collection Module allows you to do that.

Another real world example where I’m using this is I am building a web application that have an estimating system, the estimate has a title but there are multiple sections within that estimate and each estimate section may have a title of its own, a description amount value or a time value, something like that. It gets a little more complicated than that but its basic idea to use the same module and allows you to build out a content type using the field collection module and a grouping of fields.

So go ahead and give it a try and see what you think, if you’re building web applications it’s probably not something you’re going to use on a basic Drupal site or a basic business website but it is something if you’re building any type of web application where you need to manage multiple content types and multiple field groupings it can definitely be a big time saver. So go ahead and give it a try and as always, thanks for watching.