One of my favourite things lately in Drupal 6 has been CCK 3, and more specifically, the Content Multigroups sub-module. Basically this allows you to create a fieldset of various CCK fields, and then repeat that multiple times. For example, I use it on this site whist creating invoices for clients. I have a fieldset called 'Line Item', containing 'Description', 'Quantity' and 'Price' fields. With a standard fieldset, I could only have one instance of each field - however, using a multigroup, I can create multiple groups of line items which I then use within the invoice.
But at the time of writing this, there is no CCK 3 version for Drupal 7. So, I created the same thing using Field Collection and Entity modules.
With the modules uploaded and enabled, go to admin/structure/field-collections and create a field collection.

With the module enabled, you can go to your content type and add a Field Collection field. By default, the only available Widget type is 'Hidden'.

Next, go to admin/structure/field-collections and add some fields to the field collection - the same way that you would for a content type. For this collection is going to contain two node reference fields - Image and Link.

With the Field Collection created, I can now add it as a field within my content type.
Whilst this works perfectly, the field collection is not editable from the node edit form. You need to load the node, and the collection is displayed here with add, edit, and delete buttons. This wasn't an ideal solution, and I wanted to be able to edit the fields within the collection from the node edit form - the same way as I can using multigroups in Drupal 6.

After some searching I found a link to a patch which when applied adds a 'subform' widget type to the field collection field and allows for it to be embedded into, and editable from within the node form. Going back to the content type fields page, and clicking on 'Hidden' (the name of the current widget), I can change it to subform and save my changes.

With this change applied, when I go back to add or edit a node within this content type, my field collection will be easily editable directly within the form.

Comments
Update: During the time
Update: During the time between me writing and publishing this Blog post, the mentioned patch has been included within the latest development version of Field Collection module - http://drupal.org/node/964728.
Thanks much for this write
Thanks much for this write up! I've been eyeing Field Collections hoping it would mature to be a fit for my use cases. Looks like it's almost there! I have come across some bugs as of this writing (https://drupal.org/node/1263840) but I'm sure those will be shaken out after not too long.
Hi Jeffrey, Thanks for your
Hi Jeffrey,
Thanks for your comment!
Yes, especially now that the patch that I mentioned has been committed to the latest development release, it makes the process that bit simpler.
Since writing this point, I've already used the same method on several large production Drupal 7 sites with no issues.
Thanks,
Oliver
Very helpful!
Very helpful!
Thanks, Oliver! I just
Thanks, Oliver! I just installed the 7.x-1.0-beta2 version of the Field Collection module, and without having to apply a patch, was able to click on the "Hidden" widget type and choose instead "Embedded". This allowed me to edit the Field Collection values within my Edit node form and add as many entries of the Field Collection as I needed.
Hello! I have a node with
Hello!
I have a node with field-collection fields.
I need to display the data in a block with some custom HTML.
Do you happen to know how to create a block with custom HTML that displays the node fields with collection fields?
Normally I'd create that block in Views but since field-collection is in a play. I get the feeling that in Views all the collection data is in one field... and this not really visually themeable.
Thanks,
very interesting article! I
very interesting article! I will follow your themes.
Can I subscribe to your posts on Twitter or on your Facebook profile?
You made some good points
You made some good points there. I did a search on the topic and found most people will agree with your blog.
Thnx for the info.
Thnx for the info.
What if you wanted to make the line items entities in their own right, with an M:N relationship?
E.g.: authors and books. Each author can write many books, and each book can be written by many authors. Want a subform on, say, books. User could select an existing author, then another, then another. Or add a new author if there was not an existing record for an author. That would add a new author entity. That entity could be referenced from different books.
Kieran
Add new comment