OPC IT. Managed services and web development in Canberra.

Drupal 8 Migrate Source CSV - Migrating Structured Taxonomy Terms

Date: 
28 September 2016
Authored by: 
Stephen Ollman

In this series we'll be looking at Drupal 8 and many of its powerful features.

Today we'll be migrating structured taxonomy terms into a site using Drupal 8's core Migrate module and an associate contributed module called 'Migrate Source CSV'.

The aim here is to import/migrate a bunch of terms into a Drupal 8 site vocabulary using a relatively simple CSV file.

To start with we need five things:

  1. The 'Migrate' module enabled
  2. The 'Migrate Source CSV' module installed and enabled
  3. A CSV file containing our term data
  4. A configuration import script
  5. Drush to actually kick-off the migration import of the data

I'll skip steps 1 & 2 as they are standard module enabling steps.

3. The CSV file

Let's assume that we want to import a bunch of taxonomy terms into a vocabulary called 'my_vocab', as structured below:

Group A
-Division A
--Branch A
---Team A
---Team B
---Team C
--Branch B
---Team D
-Division B
--Branch C
---Team E
---Team F
---Team G

We would create a normal CSV file (myterms.csv) that looks like this:

UUID VID NAME PARENT
100 my_vocab Group A  
101 my_vocab Division A 100
102 my_vocab Branch A 101
103 my_vocab Team A 102
104 my_vocab Team B 102
105 my_vocab Team C 102
106 my_vocab Branch B 101
107 my_vocab Team D 106
108 my_vocab Division B 100
109 my_vocab Branch C 108
110 my_vocab Team E 109
111 my_vocab Team F 109
112 my_vocab Team G 109


Once created save the 'myterms.csv' file to the sites pubic:// folder, which is defined in the 'File System' configuration page at '/admin/config/media/file-system'
Typically: sites/{sitename}/files

4. The Configuration Import Script

Next, navigate to: Configuration / Configuration synchronization / Import / Single item (/admin/config/development/configuration/full/import)

Then under the 'Configuration type' dropdown, select 'Migration'

Paste in the following configuration script, and click the 'Import' button:

id: import_terms
label: Migrate terms from the source CSV to taxonomy terms
migration_group: migrated_terms
source:
   plugin: csv
   path: public://myterms.csv
   header_row_count: 1
   keys:
      - uuid
   column_names:
     0:
       uuid: UUID
     1:
       vid: VID
     2:
       name: NAME
     3:
       parent: PARENT
destination:
   plugin: entity:taxonomy_term
process:
   vid: vid
   name: name
   parent:
      plugin: migration
      migration: import_terms
      source: parent

5. The Drush command

Open up a command terminal and at your site root folder enter: drush migrate-import import_terms

A list of available Drush Migrate commands can be found here: https://www.drupal.org/node/1561820

If you receive an error you may need to change the 'id' in order to run the drush migrate-import command again.

If successful you should receive a response indicating that 13 items were imported.