Using Drupal 8's Migrate API, how can I map a nested (hierarchical) taxonomy term to the imported entity? We have a vocabulary of nested (hierarchical) category terms on a site and are using the Migrate API (via CSV) to import product entities that each have a category term.

The problem is that some of the nested term names are repeated so the importer would not know which nested term to use. For example:

PARENT 1--Child 1
PARENT 1--Child 2
PARENT 2--Child 1
PARENT 2--Child 2

As you can see, the term "Child 1" is used twice, but with different parents.

I can see two possible solutions but I don't know how to go about achieving them. So any help would be appreciated.

1) Would it be possible to include the hierarchy in a delimited field on the CSV and let Migrate create and nest all of the terms and then map the product entity term referring to the last term automatically.

For example. "Parent 1, Child, 1, Child 2"

2) Or, using a source plugin and TWO fields on the CSV, where one is the product category term and the other field is the parent and somehow have Migrate know which parent term ID to use for the category?

Currently, the category YML looks like this:

 field_prod_category:
    -
      plugin: explode
      source: WebCategory1
      delimiter: ','
    -
      plugin: entity_generate
      source:
      value_key: name
      bundle: product_categories
      entity_type: taxonomy_term
      ignore_case: true
On my project, I created a group migration with two migrations


taxonomy migration all taxonomy term.
taxonomy_parent update parent for terms have parent.


There are example code (I use the embedded_data source plugin for the sake of simplicity so that this example can be copy-pasted as a working example ) and source like that:


Migration group: migrate_plus.migration_group.taxonomy.yml

id: taxonomy
label: Import Taxonmy
description: Import Taxonomy.


Migration all term: migrate_plus.migration.taxonomy.yml

id: taxonomy
label: Import term taxonomy
migration_group: taxonomy
source:
  plugin: embedded_data
  data_rows:
    -
      id: 1
      name: 'Parent 1'
    -
      id: 2
      name: 'Children 1'
    -
      id: 3
      name: 'Children 2'
    -
      id: 4
      name: 'Children children 1'
  ids:
    id:
      type: integer
destination:
  plugin: entity:taxonomy_term
  default_bundle: custom_taxonomy
process:
  name: name


Set parent for term: migrate_plus.migration.taxonomy_parent.yml

id: taxonomy_parent
label: Set parent taxonomy
migration_group: taxonomy
source:
  plugin: embedded_data
  data_rows:
    -
      id: 2
      parent: 1
    -
      id: 3
      parent: 1
    -
      id: 4
      parent: 2
  ids:
    id:
      type: integer
destination:
  plugin: entity:taxonomy_term
  default_bundle: custom_taxonomy
  overwrite_properties:
    - parent
process:
  tid:
    plugin: migration_lookup
    migration: taxonomy
    source: id
  pid:
    plugin: migration_lookup
    migration: taxonomy
    source: parent
  parent: '@pid'


and result: