Using the Migrate Source CSV plugin

Last updated on
18 June 2017

How to import a simple CSV file with Migrate Source CSV plugin

This very basic example will show you how to import Title and Body into the Article content type from a CSV file.

  1. Download Migrate Source CSV and enable it.
  2. Download Migrate Plus and enable it
  3. Download Migrate Tools and enable it.
  4. Ensure you're using the latest version of Drush.
  5. Create a sample CSV-file called acme_articles.csv with the following content:
    1,title 1,some body text 1 
    2,title 2,some body text 2 
    3,title 3,some body text 3
  6. Navigate to Administration > Configuration > Development > Synchronize (admin/config/development/configuration/single/import), select Migration under Configuration type and enter this CSV-import configuration into the Paste your configuration here window (note the absolute path to the .csv file) , and hit import:
    id: basics
    label: Import articles
      - ACME import
      plugin: csv
      # Full path to the file.
      path: '/path/to/file/acme_articles.csv'
      # Column delimiter. Comma (,) by default.
      delimiter: ','
      # Field enclosure. Double quotation marks (") by default.
      enclosure: '"'
      # The number of rows at the beginning which are not data.
      header_row_count: 1
        - id
      # Here we identify the columns of interest in the source file. 
      # Each numeric key is the 0-based index of the column. 
      # For each column, the key below is the field name assigned to 
      # the data on import, to be used in field mappings below. 
      # The value is a user-friendly string for display by the 
      # migration UI.
          id: 'Unique Id'
          title: 'Title'
          body: 'Post body'
      title: title
      body: body
        plugin: default_value
        default_value: article
      plugin: entity:node
  7. Go to your terminal and execute the command drush migrate-import basics to import the three example articles. If there is an error in your yml-configuration, and you try to resubmit it, you might have to change the id from basics to something else for it to get accepted (this is because by default, Drupal 8 doesn't allow existing configurations to be overwritten; you can however export the migration configuration (contains a uuid) and reimport the changed configuration (with the exported uuid) with the same id).

How to override the path to the CSV-file

using drupal console and a migration named basics:

drupal config:override migrate_plus.migration.basics source.path /path-to.csv

How to override the delimiter and enclosure to the CSV-file

The system uses the comma (,) and double quotation marks (") as enclosure by default.

You can change this by adding these lines with your custom delimiter and enclosure in the migration definition file migrate_plus.migration.basics.yml

  plugin: csv
  path: '/path/to/file/acme_articles.csv'
  # Column delimiter. Comma (,) by default.
  delimiter: ','
  # Field enclosure. Double quotation marks (") by default.
  enclosure: '"'
  header_row_count: 1
    - id

View or edit existing migration configuration

drupal config:edit migrate_plus.migration.basics

You will need to run drush cr to rebuild the cache before importing the file again. You'll need to rollback first too for any changes to "process" section (e.g. changes to default values) to be applied.