I'm doing a site upgrade for a client from Drupal 6 to Drupal 9. This time I decided to dive deeper into the Drupal Migrate API and use it to migrate content. Everything was going smoothly until I ran into image migration. I migrated the files themselves without problems, as well as the content, the only thing I couldn't do was link the file entities to the content entities. On the old (Drupal 6) website they were in the field_news_images, on the new one the field is called field_images.
The base of the Migrate API is made up of yaml files, the structure of which is quite easy to understand. It soon dawned on me that I had to add something like this to the content migration yaml:
field_images:
plugin: sub_process
source: field_news_images
process:
target_id: fid
It means that the ID of the file (image) from the field on the Drupal 6 site will be transferred to the corresponding field on the Drupal 9 site. It worked, it just had one flaw. The IDs of the files on the old and new website did not match, and therefore the migration script assigned non-existent entities to my content. It was therefore necessary to make one more small change and add the following little thing to the migration script for the files:
fid:
-
plugin: get
source: fidIt means that when migrating files, we will also migrate their ID (fid) and thus the ID will be the same on the old and on the new website. I reverted the migrated files and content and re-migrated first the files, then the content and voila! Files were correctly associated with content.
Even so, it still bothered me if is there a way how to migrate files without having to migrate their IDs as well. It is for the migration_lookup plugin, which can read data from another migration. The relevant code for content migration will then look something like this:
field_images:
plugin: sub_process
source: field_news_images
process:
target_id:
plugin: migration_lookup
migration: upgrade_d6_ksa_file
source: fidIt means something like this: I would like the content of the field_news_images field in the field_images field, and when you migrate it, use the ID under which you migrated this file as part of the upgrade_d6_ksa_file migration for the new image.
Comments