Contrib process plugin: entity_generate

Last updated on
22 June 2023

The entity_generate plugin, provided by the contributed Migrate Plus module, is used to match existing entities (by entity type, bundle type, and value_key) and generate entities that don't yet exist.

The most common use is for taxonomy term reference fields.

Taxonomy Terms Example with Documentation

Here's a taxonomy term example using all of the possible configuration parameters (spacing and commenting only for documentation purposes):

process:
  # Field to populate
  field_tags:
    # Plugin to use.
    plugin: entity_generate

    # Field from source configuration.
    source: 'Tag'

    # Name of the value in the entity to use for look-up.
    # Values in this field are compared against values from the 
    # source field specified above for "source".
    value_key: name

    # For entities that support bundles: Name of the key that contains the bundle type.
    bundle_key: vid

    # For entities that support bundles: Machine name of the bundle.
    bundle: tags

    # Type of entity.
    entity_type: taxonomy_term

    # Set to true to ignore case when looking up values through value_key.
    ignore_case: true

    default_values:
      # Default values to apply to newly created entities.
      # This is ignored if the target entity already exists.
      # Each key is a machine name of a field in the target entity, and
      # the value is a constant that is used as a default value for that
      # field. The value specified here is only used if the value is not
      # populated by a value from the source (e.g., via "values" below).
      status: 1

    values:
      # Values to populate in newly created entities based on source fields.
      # This is ignored if the target entity already exists.
      # Each key is a machine name of a field in the target entity, and
      # the value is the name of the source field from which the value is
      # sourced.
      description: 'Tag description'

Simpler Taxonomy Terms Example

Example usage with minimal configuration for term import, for example to Tags field in Article content type:

process:
  field_tags:
    plugin: entity_generate
    source: tags

Example of Generating/Looking Up Nodes by Title

Node generation is similar to taxonomy terms, but the bundle_key for nodes is "type" (for the content type of the node):

process:
  field_related_articles:
    plugin: entity_generate
    source: 'title'
    entity_type: node
    bundle: article
    value_key: title
    bundle_key: type
    default_values:
      title: 'Title missing'
    values:
      title: 'title'

Example of Generating/Looking Up Multiple Taxonomy Terms

If you have multiple terms in you source, for example "tags": "great;helpful;awesome" you can split them up with the Migrate plugin Explode, where the data is separated before being piped on to entity_generate and get created:

process:
  field_tags:
    - plugin: explode
      source: tags
      delimiter: ';'
    - plugin: entity_generate

Example of Generating Users

The following example was taken from a migration for a comment:

process:
  uid:
    - plugin: skip_on_empty
      method: row
      source: 'Author email'
      message: 'Author email is required but missing.'
    - plugin: entity_generate
      entity_type: user
      value_key: mail
      values:
        name: 'Author email'

Help improve this page

Page status: No known problems

You can: