Entity Lookup & Generate migrate process plugins

Posted by Ada Hernández on January 25, 2017

Thanks to the drupaleros community, Drupal 8 has two new plugins added in the migrate plus module: entity_lookup and entity_generate. These can be used to lookup and create an entity if it does not exist. I will show you a way in which we can implement these plugins and we will see for what applications they can be used.

entity_lookup:  As its name says, it looks if there is any previously created entity. If it does not exists it returns NULL, but if it exists it returns the value of that entity. This is a base plugin that is used by entity_generate. For your configuration we have a few options:

Here is an example of the complete configuration of an Entity reference field in our migration template from a csv file:

field_language:
   source: languages
   plugin: entity_lookup
   value_key: name
   bundle_key: vid
   bundle: languages
   entity_type: taxonomy_term
   ignore_case: true

Or we can have a minimum of configuration. If you have configured the field_languages correctly, the lookup plugin can introspect the field configuration for you (yeah for magic) and determine the entity type (taxonomy_term) and vocabulary (languages).

field_language:
  source: languages
  plugin: entity_lookup

Here is an example of looking for a file or image:

field_photo:
  source: photo
  plugin: entity_lookup

entity_generate This is a very important plugin for our migrations; it saves a good amount of time. Now we can create or generate our related entities automatically. If it is not created yet, entity_generate extends from the base plugin entity_lookup and creates it.

This plugin does not need configuration, but if there are fields in the generated entity that are required or need some predetermined value, it can be provided through a configuration option default_values.

Example of use with the configuration default_values:

field_language:
  plugin: entity_generate
  source: languages
  default_values:
    description: 'Default description'
    field_text‎: 'Default long description'

And the short way without configuration

field_language:
  plugin: entity_generate
  source: languages

Well these are examples of how we can use these plugins in our migrations, I hope they are useful.

Are you looking for help with a Drupal migration or upgrade? Regardless of the site or data complexity, MTech can help you move from a proprietary CMS or upgrade to the latest version–Drupal 8.

Write us about your project, and we’ll get back to you within 48 hours.