Migrate Field Values Into New Referenced Entity Using Migrate Lookup

During site upgrades, we often need to make changes to the content types and fields. In this example, there is a Book content type with fields for the author name and bio. These should be imported into a new Author content type.

First, I copied the migration that was generated to migrate the content with the source fields. I stripped it down to the fields needed to import on Author, and changed the nid field to use entity_lookup, from the Migrate Plus module.

process:
  nid:
    -
      plugin: entity_lookup
      value_key: field_author_name
      bundle_key: type
      bundle: author
      entity_type: node
      source: field_author_name

The trick here is that if an existing author is not found with a matching field_author_name, it will import a new one. If it is found, it will overwrite the existing one to avoid creating duplicates.

Tags