Populating a taxonomy field through a Drupal migration.

Pasan Gamage
3 min readJun 4, 2021

--

Today's story is about how to populate a taxonomy term field in a node via a migration.

I’ll be using an XML file as my data source, and I’m using migrate, migrate plus and migrate tools modules to make life easier.

Prerequisite

  • Create a taxonomy vocabulary named Published Date
  • Add a new entity reference field with reference type set to the new vocabulary having machine name published_date in my custom XML node type
  • Make sure to select Create reference entities if they don’t exist

A simplified migration file is shown below.

id: xml_migrate
label: 'XML Migrate - Level 1'
migration_group: xml_migrate_level_1
migration_tags:
- xml_migrate_tag_1
source:
plugin: url # From migrate_plus module
data_fetcher_plugin: file # From migrate_plus module
data_parser_plugin: simple_xml # From migrate_plus module
urls:
- modules/custom/xml_migrate/sources/my-xml-data-source.xml
item_selector: /node()[self::subordleg or self::act]
fields:
- name: leg_title
label: 'Leg Title'
selector: '@title
- name: publication_date
label: 'Publication Date'
selector: '@publication.date'
ids:
leg_id:
type: string
publication_date:
type: string
process:
title:
plugin: default_value
source: leg_title
default_value: 'New Node'
field_leg_published_date:
- plugin: entity_generate # You can use different YML syntaxes
source: publication_date
entity_type: taxonomy_term
bundle_key: vid
bundle: published_date
value_key: name
destination:
plugin: 'entity:node'
default_bundle: xml # Node Type
migration_dependencies: null

entity_generate Plugin break down

  • plugin

We are using migrate plus process plugin entity_generate to create new terms to the Publication Date vocabulary if terms do not exist.

  • Source

The field that was created named publication_date which will be populated by the data parser plugin using the given selector ‘@publication.date’

  • entity_type

The entity type for taxonomy is taxonomy_term

  • bundle

The machine name of the created taxonomy vocabulary. In this case it is published_date

  • value_key

Value to compare in the bundle. This is a column name in the taxonomy_term_field_data table where the term values are stored. Usually it will be in name column

  • bundle_key

Bundle key value. This is a column name in the taxonomy_term_field_data table where the term keys are stored. Usually it will be in vid column

And that's all to it !

I have not tried how it will work if you have the cardinality of the field_leg_published_date more than 1. So let me know if anything needs to be changed in the migration to allow that.

Resources

https://www.drupal.org/docs/8/api/migrate-api/migrate-process-plugins/contrib-process-plugin-entity_generate

https://www.drupal.org/docs/8/api/migrate-api/migrate-destination-plugins-examples/migrating-taxonomy-terms

--

--

Pasan Gamage
Pasan Gamage

Written by Pasan Gamage

Backend Developer | Motorbike enthusiast

No responses yet