Migrating CSV Data - Drupal 8.5.3

By peterm, 27 June, 2018

Tags

Building Address Project

Given a CSV file and a matching content type, here's how we get the data migrated into Drupal.

The following modules will be required:

Migrate, Migrate Plus, Migrate Source CSV, Migrate Tools, Migrate Drupal UI

Note that in this example we will import latitude, longitude into a geofield.

I had some drush migrate-import errors and needed to work with composer to get he latest dev version installed for Migrate Tools.

drush migrate-import mailstops_import

drush migrate-rollback mailstops_import

  

This one was helpful for removing broken migrations I had created along the way:

drush config-delete

* Create a migration group at /admin/structure/migrate

* Create the CSV file

    * The CSV will need a column inserted for 'id'. This can be a unique number created via Excel.

    * The quoting of some or all data may be required. Note that you'll need to specify delimiter and quoting in the YAML file

* Create the YAML file

    * To get a head start, go to /admin/config/development/configuration/single/export

    * Select Migration and the migration you just created in step 1

    * Copy the outline into a text editor and modify it. This is the step where you'll end up needing to do research if you're importing geofield or location data to understand if you are relying on a plugin.

Sample YAML 

uuid: 0a672ec8-3f52-42e3-a1db-eaab8c7f26ba

langcode: en

status: true

dependencies: {  }

id: mailstops_import

class: null

field_plugin_method: null

cck_plugin_method: null

migration_tags: null

migration_group: null

label: 'Import Mailstops'

source:

  plugin: csv

  path: /var/www/html/mailstops.csv

  delimiter: ','

  enclosure: ''

  header_row_count: 1

  keys:

    - id

  column_names:

    -

      id: id

    -

      title: title

    -

      address: address

    -

      room: room

    -

      city: city

    -

      state: state

    -

      zip: zip

    -

      latitude: latitude

    -

      longitude: longitude

process:

  title: title

  field_address: address

  field_room: room

  field_city: city

  field_state: state

  field_zip: zip

  field_location:

    plugin: geofield_latlon

    source:

      field_location/lat: latitude

      field_location/long: longitude

  type:

    plugin: default_value

    default_value: mailstop

destination:

  plugin: 'entity:node'

migration_dependencies: null

Sample CSV

id,title,address,room,city,state,zip,latitude,longitude

1,ACADEMIC PERSONNEL OFFICE,500 Kerr Road,499,Santa Cruz,California,95064,36.9954,-122.061

2,ACADEMIC SENATE,500 Kerr Road,125,Santa Cruz,California,95064,36.9954,-122.061

3,ACCOUNTING OFFICE,2300 Delaware Ave.,Suite A208 A212,Santa Cruz,California,95064,36.9563,-122.059

4,ACE PROGRAM,606 Engineering Loop,199,Santa Cruz,California,95064,37.0002,-122.063