1

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 into Drupal 8.

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
    • Child 2
  • PARENT 2
    • Child 1
    • 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"

  1. 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

2 Answers 2

2

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: enter image description here

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:

enter image description here

2

Unsure if this works in your situation but you can import both parents an children in the same migration, providing that the parent terms are being listed before the child terms. As follows:

id: custom_taxonomy_term
label: 'Custom taxonomy term'
source:
  plugin: embedded_data
  data_rows:
    -
      name: test 1
      parent: ''
    -
      name: test 2
      parent: test 1
    -
      name: test 3
      parent: test 1
  ids:
    name:
      type: string
  constants:
    vocabulary: custom
process:
  name: name
  vid: constants/vocabulary
  pid:
    plugin: migration_lookup
    migration: custom_taxonomy_term
    source: parent
  parent: '@pid'
destination:
  plugin: 'entity:taxonomy_term'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.