I recently faced an issue where I was importing terms. All the terms are working fine, but the issue came when the language setting is selected as “localize” for the vocabulary.
The process which I followed was I installed migrate module, migrate upgrade, exported the config files. It created the config file for migrating taxonomy for all vocabulary and in addition to that:
upgrade_d7_taxonomy_term_localized_translation and upgrade_d7_taxonomy_term_translation
First, understand how localized terms are stored.
In this, “Volunteers in Action” has term id 487, and i18n_tsid as 21, for the french term, “Benevole en action”, in D7, it has a term id of 488, and i18n_tsid as 21.
When we import taxonomy terms, what it will do is create an entry for both the terms 487 and 488, but there will not be linking as we have in Drupal 7.
To resolve this, what we can do is, we first have to block migration from importing those terms for which i18n_tsid != 0, to do this we will use hook_migrate_prepare_row. Suppose our vocab name is education, for which localize is enabled, for it we will write code as below.
In the above code, “upgrade_d7_taxonomy_term_education”, is the name of our config migration file, which we have created using the migrate_upgrade module, we are first getting a value of i18n_tsid, and checking if it is not equal to 0, then we are skipping the term or row.
After that when we run the migration for “upgrade_d7_taxonomy_term_education”, it will import all taxonomy terms except those for which i18n_tsid is not equal to 0.
Now for importing localize terms and their dependency, we will create a source plugin, which will get all terms for which i18n_tsid !=0 from all vocabulary.
We will create a folder under our module “src/Plugin/migrate/source/d7”. We will then create the file name “TermTranslationSet.php”.
In the above code (you can copy this code here if you need it), we are querying the db, to get all rows for which i18n_tsid != 0, above query will give us the output, like below:
Here you can see that for each corresponding ttd2_tid, we have 2 rows of tid, which have different languages. So we will be importing ttd2_tid first row, and the second row will be created as the translation for the first row. Taking the first 2 rows as an example, we will be creating a term with tid 487 and we will add tid 488, as its translation in french.
The next step will be to create a custom migration config file for migrating the terms which we got from above.
We will create a migration config file with name migrate_plus.migration.upgrade_d7_taxonomy_term_translation_set.yml
In the above code, (you can copy this code here if you need it), you can see that we are creating a term with tid as ttd2_tid, which is the same for the row.
We will now run:
drush migrate-import upgrade_d7_taxonomy_term_translation_set
This will create our term and its translation.