Media Migration With Sequential IDs

Default Drupal migrations import original entity IDs, but can be modified to reset IDs.

Follow the instructions for generating migrations using Drush, but enable the Media Migration module first to get media migrations.

upgrade_d7_file

These lines configure the file migration to import existing IDs. Delete these lines to import files with sequential IDs.

fid:
  -
    plugin: get
    source: fid

upgrade_d7_file_entity_TYPE

Media migrations need to be adjusted to lookup the file id for the reference field.

field_media_image/target_id:
  -
    plugin: get
    source: fid
field_media_image/target_id:
  -
    plugin: migration_lookup
    migration: upgrade_d7_file
    no_stub: true
    source: fid

Media migrations have to track ids of imported files and media so they can be converted when processing body text.

The generated media config works fine out of the box, but is worth explaining to help troubleshoot.

# Used for checking changes on subsequent imports.
track_changes_uuid:
  -
    plugin: migration_lookup
    source: fid
    migration:
      - upgrade_d7_file_entity_image_public
      - upgrade_d7_file_entity_document_public
      - upgrade_d7_file_entity_default_public
      - upgrade_d7_file_entity_audio_public
      - upgrade_d7_file_entity_video_public
      - upgrade_d7_file_entity_video_youtube
      - upgrade_d7_file_entity_video_vimeo
    no_stub: true
  -
    plugin: skip_on_empty
    method: process
  -
    plugin: migmag_get_entity_property
    entity_type_id: media
    property: uuid

# Generated uuid used when imported media is not found.
# See the database table media_migration_media_entity_uuid_prophecy.
oracle_uuid:
  -
    plugin: media_migrate_uuid
    source: fid
    
# Uses null coalesce to get existing uuid or generated one if not found.
uuid:
  -
    plugin: log
    source: fid
  -
    plugin: null_coalesce
    source:
      - '@track_changes_uuid'
      - '@oracle_uuid'

upgrade_d7_node_TYPE

Formatted text fields are processed with media_wysiwyg_filter and image_tag_to_embed to convert embedded json tokens and image tags to media embeds.

When imported ids are changed, migration lookups are needed.

The media_wysiwyg_filter plugin has media_migrations and file_migrations config that are not set in the generated migrations.

img_tag_to_embed has migrations config for media migrations.

body/0/value:
  -
    plugin: get
    source: body/0/value
  -
    plugin: media_wysiwyg_filter
    media_migrations:
      - upgrade_d7_file_entity_image_public
      - upgrade_d7_file_entity_document_public
      - upgrade_d7_file_entity_default_public
      - upgrade_d7_file_entity_audio_public
      - upgrade_d7_file_entity_video_public
      - upgrade_d7_file_entity_video_youtube
      - upgrade_d7_file_entity_video_vimeo
    file_migrations:
      - upgrade_d7_file
  -
    plugin: img_tag_to_embed
    migrations:
      - upgrade_d7_file_entity_image_public
      - upgrade_d7_file_entity_document_public
      - upgrade_d7_file_entity_default_public
      - upgrade_d7_file_entity_audio_public
      - upgrade_d7_file_entity_video_public
      - upgrade_d7_file_entity_video_youtube
      - upgrade_d7_file_entity_video_vimeo

File fields are generated with config that does a migration lookup, so no change is needed.

You can speed up the migration lookup by listing the migrations in order of their use in the field.

field_pdf_file:
  -
    plugin: media_migration_delta_sort
    source: field_pdf_file
  -
    plugin: sub_process
    process:
      target_id:
        plugin: migration_lookup
        source: fid
        migration:
          - upgrade_d7_file_entity_document_public
          - upgrade_d7_file_entity_image_public
          - upgrade_d7_file_entity_default_public
          - upgrade_d7_file_entity_audio_public
          - upgrade_d7_file_entity_video_public
          - upgrade_d7_file_entity_video_youtube
          - upgrade_d7_file_entity_video_vimeo
          - upgrade_d7_file_plain_text_public
        no_stub: true
Tags
Modules