Problem/Motivation
I have a migration that is migrate English and Spanish content. It has multiple steps:
- Migrating files and media
- Migrating Taxonomies first in English and then in Spanish and linking the translations
- Migrating Content first in English and then in Spanish and linking the translations
The majority of it works but I have discovered that when migrating the Spanish content, the fields which are entity relationships to taxonomy terms do not get migrated.
The snippet of the English content migration YAML which works is:
....
process:
....
field_themes:
plugin: migration_lookup
migration: themes
no_stub: true
source: themecats
....
destination:
plugin: entity:node
default_bundle: resource
....
The snippet of the Spanish content migration YAML which doesn't work is:
....
process:
....
langcode:
plugin: default_value
default_value: 'es'
....
field_themes:
plugin: migration_lookup
migration: themes_es
no_stub: true
source: themecats
....
destination:
plugin: entity:node
default_bundle: resource
translations: true
....
The English migration will create the nodes and link the terms in field_themes
. The Spanish migration will create the nodes but the terms will not be linked.
I have done some debugging and discovered that if I get the value of field_themes
before the entity is saved in the save() function in /core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php then the structure of the value is different for English and Spanish.
e.g. var_dump($entity->get('field_themes')->getValue());
produces
English
array(2) {
[0]=>
array(1) {
["target_id"]=>
string(4) "1371"
}
[1]=>
array(1) {
["target_id"]=>
string(4) "1385"
}
}
Spanish
array(1) {
[0]=>
array(2) {
[0]=>
string(4) "1371"
[1]=>
string(2) "es"
}
}
Notice that for English the array is keyed on target_id
but the Spanish are keyed on integers.
By adding (hacking!) code into the save()
function and using $entity->get('field_themes')->getValue()
to retrieve the values, process them into a new array, and then set them using $entity->get('field_themes')->setValue()
. The value returned now contained an array item keyed on target_id
and the taxonomy terms were created albeit in English not Spanish.
This test suggests to me that when creating the values for the Spanish fields the migration module code isn't creating the structure correctly. Unfortunately, I don't know enough to dig deeper to find where that might be happening.