Using the Migrate Source CSV plugin
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.
- Download Migrate Source CSV and enable it.
- Download Migrate Plus and enable it
- Download Migrate Tools and enable it.
- Ensure you're using the latest version of Drush.
- Create a sample CSV-file called
acme_articles.csvwith the following content:
Id2,title,body 1,title 1,some body text 1 2,title 2,some body text 2 3,title 3,some body text 3
- 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 migration_groups: - ACME import source: 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 keys: - Id2 # 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. column_names: 0: id: 'Unique Id' 1: title: 'Title' 2: body: 'Post body' 3: process: title: title body: body type: plugin: default_value default_value: article destination: plugin: entity:node
- Go to your terminal and execute the command
drush migrate-import basicsto 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
basicsto 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
How to override the path to the CSV-file
using drupal console and a migration named
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
... source: 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 keys: - Id2 ...
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.