Contrib process plugin: entity_generate
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'Example of Generating 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
value_key: name
bundle_key: vid
bundle: tags
entity_type: taxonomy_termA single term import can be done like this:
process:
field_photo_category:
plugin: entity_generate
source: photo_category
value_key: name
bundle_key: vid
bundle: image_category
entity_type: taxonomy_termCreate terms and insert custom field values at the same time
Vocabulary terms can have extra fields, and it's possible to generate a non-existing term, while at the same time inserting a value into another field. This example shows how to loop over a nested JSON-file, create a non-existing term, and populate a multi-value field.
JSON source:
[{
"companies": [
{
"company_name": "Company INC.",
"company_link": "https://example.org/"
},
{
"company_name": "ABC Company",
"company_link": "https://example.com/"
}],
[...]Combining sub_process and entity_generate:
process:
taxonomy_vocabulary_4:
plugin: sub_process
source: companies
process:
target_id:
plugin: entity_generate
source: company_name
value_key: name
bundle_key: vid
bundle: vocabulary_4
entity_type: taxonomy_term
values:
field_link/value: company_link
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 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
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion