While migrating Taxonomy Terms we've to consider few basic settings. Like whether our destination database (drupal 8 instance) is already containing some old data or not. Whether we need to rollback post migration ... If we follow migration strategy and develop our code based on that we can easily achieve that.
Here we'll be introduced with dedupe_entity . It's a process plugin with two configuration keys and these are entity_type and field. It'll rename the field value automatically while already exist. This facility is available from Migrate Module. For more detail please refer here.
Here you can find an working example of Taxonomy - Term - Vocabulary migration working copy.
Please refer the Github code for that.
Migration of Vocabulary:
Migration of Vocabulary is done with these files below.
migrate_custom\src\Plugin\migrate\source\Vocabulary.php
migrate_custom\config\install\migrate_plus.migration.custom_taxonomy_vocabulary.yml
Please refer Vocabulary.php for code reference.
In .yml file we'll define four definitions as required i.e. id, source, process, destination.
So the .yml file will look like this -
id: custom_taxonomy_vocabulary
label: Drupal 7 taxonomy vocabularies
migration_group: custom
dependencies:
enforced:
module:
- migrate_custom
source:
plugin: custom_taxonomy_vocabulary
process:
vid:
-
plugin: machine_name
source: machine_name
-
plugin: dedupe_entity
entity_type: taxonomy_vocabulary
field: vid
length: 32
label: name
name: name
description: description
hierarchy: hierarchy
module: module
weight: weight
destination:
plugin: entity:taxonomy_vocabulary
Here the significant plugin in process is "dedupe_entity". This Ensures value is not duplicated against an entity field (Taxonomy). If the value is
foo
and there is already an entity where the field value is foo
, then the plugin will return foo1
. For more detail please refer core/modules/migrate/src/Plugin/migrate/process/DedupeEntity.php
Migration of Terms:
migrate_custom\src\Plugin\migrate\source\Term.php
migrate_custom\config\install\migrate_plus.migration.custom_taxonomy_term.yml
Here in Term.php we fetch Term data from 'taxonomy_term_data' and do the needful process before setting the source property.
id: custom_taxonomy_term
label: Drupal 7 taxonomy terms
migration_group: custom
dependencies:
enforced:
module:
- migrate_custom
source:
plugin: custom_taxonomy_term
process:
tid: tid
vid:
plugin: migration
migration: custom_taxonomy_vocabulary
source: vid
name: name
description: description
weight: weight
parent:
# -
# plugin: skip_on_empty
# source: parent
-
plugin: migration
migration: custom_taxonomy_term
changed: timestamp
destination:
plugin: entity:taxonomy_term
migration_dependencies:
required:
- custom_taxonomy_vocabulary