Skip to main content

Programmatically Adding a Field Collection Item to a Node in Drupal

Front-end Development
Back-end Development

I recently ran into a situation where I had content in an image field that I needed to migrate to a field collection.  The field collection had the same image field, but also had a link field and a few text fields.  


  1. Load the node that the field collection item will be saved to.
  2. Create the field collection item.
  3. Prepare the fields that will be populated.  For the "link" field and others, I didn't have any content yet so creating the structure was sufficient.  For the file, loading it as an array with the file_load() function was all that was needed.
  4. Load the field collection fields with the link and file content.  Once this is done, all that is left is to add the the field collection item to the node, save it, and save the node.

Full code:

Other Resources

Here are some other resources I found helpful on this topic: