Drupal - How to migrate multilingual content from CSV to Drupal 8?
you need both migration-tasks, the yml files in the migration folder:
migrate.migration.external_translated_test_node.yml
migrate.migration.external_translated_test_node_translation.yml
Then in the Plugin (MigrateExternalTranslatedTestSource.php), you need to read your .csv (in initializeIterator()
) into the structure of the import array:
/**
* The data to import.
*
* @var array
*/
protected $import = [
['name' => 'cat', 'title' => 'Cat', 'lang' => 'English'],
['name' => 'cat', 'title' => 'Chat', 'lang' => 'French'],
['name' => 'cat', 'title' => 'Gato', 'lang' => 'Spanish'],
['name' => 'dog', 'title' => 'Dog', 'lang' => 'English'],
['name' => 'dog', 'title' => 'Chien', 'lang' => 'French'],
['name' => 'monkey', 'title' => 'Monkey', 'lang' => 'English'],
];
Migrate from CSV basics
As stated elsewhere, the migration code has only recently (as of 8.2.x or 8.3.x) seemed to settle down and there are loads of old, outdated and incorrect instructions in blog posts around the web. That being said, I've had about 75% success following the instructions on these two sets of blog posts from Lucas Hedding and Ada Hernández at Mtech.
https://www.mtech-llc.com/blog/lucas-hedding/migrating-using-csv
https://www.mtech-llc.com/blog/ada-hernandez/how-migrate-images-drupal-8-using-csv-source
There is an accompanying example codebase Lucas setup at Github that covers the basics of the first CSV blog post example, but it doesn't include any helpers with the image or file migrations.
- https://github.com/heddn/d8_custom_migrate
There is another example repository from Wunderkraut that does include a CSV example migration with user, taxonomy, articles and images migration here:
- https://github.com/wunderkraut/migrate_source_example/tree/8.2.x/modules/migrate_source_example_csv
Multilingual migration from CSV
I recommend starting with the CSV basics above, then moving to this section. You really need to have separate migration scripts for each language.
I got an additional 20% of the way there with this blog post, that outlines the details how to modify a migration yaml file to support multilingual content including, to enable translations: true
in the destination
section, to add the the langcode:
field in the process
section for proper multi-lingual entities (like nodes and terms), and to add the language:
parameter for specific multi-language field processing.
- https://blog.liip.ch/archive/2017/01/05/drupal-8-multilingual-content.html
I've found the most difficult part to be figuring out how to import file or image fields (the remaining 5% for me). Neither of the examples above demonstrate how to import image alt fields properly. In my case I have an image field that shares the image, but the alt attribute is translatable. I figured out how to import the basic alt field to the default language node on my own, but I'm having trouble getting the import to use the same fid but different alt text for other languages. The solution was figured out by user @Vasi by adding an additional field called content_translation_source
on the process step of the foreign language migration YAML. More details with complete example here:
- https://drupal.stackexchange.com/a/229874/3167
Additional Discussion
Finally, there is some discussion going on here as well:
- #2733431: Multilingual Migrations for Non-Drupal Sources