I'm writing a Drupal 8 migration from a non-Drupal site where I need to migrate news articles that have inline images in them, sometimes more than 10. I've figured I'll do this in two steps:

  1. A migration for creating file entities for each image present in the source node body.
  2. A migration that creates news article entities, and for each image encountered, look up the file entity that was created in the 1st migration and use the File Usage API to indicate the file is used in this node.

My problem is the first migration. The Migration API wants to map a single source to a single destination. I need to be able to map a single source to MULTIPLE destinations, since I will be creating as many as 10 file entities from this single source row.

Does anyone know if this is possible, and if so, are there any examples in core or contrib migrating like this?

If I can't find a way to do it this way, I'll instead just migrate everything in the same single migration, and create the file entities on the fly. I'll just have to write a custom rollback implementation to remove them as well.

1 Answer 1


I usually use D7 migrate. But from what i've read of D8 either of these approaches should work:

My problem is the first migration. The Migration API wants to map a single source to a single destination.

Option 1

In migration 1 simply import any encountered file into drupal. Then record the file id to a custom table of columns legacy_url, old_source_id, drupal_file_id for example in the complete() method of the migration.

In your second migration pass an array of drupal_file_id to your Node's file field from a select drupal_file_id from mytable where old_source_id = $old_source_id. This will attach the legacy files into the new Node. You may need still need to adjust any inline usage of images, or just use a view_mode to show the file list in D8.

Option 2

Just import articles (don't use a 1st migration for just files) and use complete() method in a migration to scan the HTML using DomDocument or simple_html_dom to find file urls and use the drupal API to save the remote file to drupal and then update the DOM with the new url. Create a custom rollback function to remove these files if needed.


Some example Drupal 8 file migration logic and classes can be seen at the following link. This may not address your exact issue, but in my opinion is good reference. http://www.jeffgeerling.com/blog/2016/migrating-20000-images-audio-clips-and-video-clips-drupal-8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.