Skip to main content
Drupalista.sk

Main navigation

  • Home
User account menu
  • Kontakt

Breadcrumb

  1. Home

Drupal Migrate API: How to migrate images

By drupalista, 3 September, 2023
Drupal Migrate API

 

 

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: fid

It 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: fid

It 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.

Tags

  • Migrate API
  • Log in or register to post comments

Comments

Language switcher

  • English
  • Slovak

Recent content

  • Quick idea: How to filter view result by content entity ID?
    2 years ago
  • How to add a new token for existing module?
    2 years ago
  • Drupal Migrate API: How to migrate images
    2 years 5 months ago
RSS feed
Powered by Drupal