How to Import Data from a CSV File to Drupal 8: The Best Tools and Approaches You Can Use

How to Import Data from a CSV File to Drupal 8: The Best Tools and Approaches You Can Use

Here's your challenge for the day: import data from a CSV file to Drupal 8! To a specific content type on your Drupal 8 website, I mean.

And it looks like you're anything but nickel and dimed in options. In modules geared to migrate data from a CSV source to Drupal 8:

  • you have the Migrate and Migrate Drupal modules 
  • you can leverage the Migrate Plus' API for migrating data stored in different sources
  • there's the Migrate Source CSV contributed module that you can use for your CSV import scenario
  • then, there's the Content Import module approach to migrating data from a spreadsheet

Which tools/modules should you use? Which approach best fits your own scenario?

Now let me give you a hand. Here's an overview of the 2 most popular solutions for importing CSV into Drupal 8:

  1. Using the “Migrate Source CSV & Migrate Tools & Migrate Plus” trio
  2. Using the Content Import Module
     

1. Import Data from a CSV File to Drupal 8: The Migrate Source CSV Module

Or, more accurately put: using the CSV plugin that this module provides.

Let's say you need to import the“Body” and “Title” content for an “Article” content type from a CSV source. Migrate Source CSV comes with a dedicated plugin enabling you to “extract” content from .csv files. And to migrate them to your Drupal 8 website.

Note: the plugin allows you to migrate all sorts of entity types from your CSV source, not just nodes!

It's a two-step process that this approach, based on 3 Drupal modulesMigrate Plus, Migrate Source CSV and Migrate Tools —  triggers :

  1. create the specific configuration 
  2. enter the “migrate-import” command in Drush

But, let's break it down into all its key steps, shall we?
 

Step 1

Download and install the Migrate Source CSV module.
 

Step 2 & Step 3

Next, download and enable the Migrate Tools and Migrate Plus modules, as well.
 

Step 4

Check whether you're using the latest version of Drush.
 

Step 5

Set up an “articles.csv” and enter the following data:

id,title,body 
1,title 1,some body text 1 
2,title 2,some body text 2 
3,title 3,some body text 3


Step 6

With the Migrate Plus module ON, you're now free to configure the entities in question:

  • Go to "Administration" > "Configuration" > "Development" > "Synchronize" 
  • Go for “Migration” as the “Configuration Type”
  • Copy and paste the YAML format migration content and hit “Import”
id: article_csv_import
label: Import articles
migration_groups:
  - ACME import

source:
  plugin:csv
  # Full path to the file.
  path: '/path/to/articles.csv'
  # Column delimiter. Comma (,) by default.
  delimiter: ','
  # Field enclosure. Double quotation marks (") by default.
  enclosure: '"'
  # The number of rows at the beginning whichare not data.
  header_row_count: 1
  # The column(s) to use as a key. Each column specified will 
  # create an index in the migration table and too many columns 
  # may throw an index size error.
  keys:
    - 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.
  column_names:
    0:
      id: 'Unique Id'
    1:
      title: 'Title'
    2:
      body: 'Post body'
process:
  title: title
  body: body
  type:
    plugin: default_value
    default_value: article

destination:
  plugin: entity:node

Note: the path to your CSV source is defined in the configuration.
 

Step 7

Now just import data from a CSV file to Drupal 8:

Use the “migrate-import” Drush command that the Migration Tools module provides you with. 

Enter:

drush migrate-import article_csv_import


… in the CLI on your server. 

Et voila!


2. Migrate Data from a CSV Source to Drupal 8: The Content Import Module 

If you're looking for a simple solution for your CSV import challenge, this is the one.

Note: this approach might not suit complex scenarios, with lots of customization and intricate Drupal fields to CSV columns mapping. In such cases, a custom solution works far better.

The “beauty” of this module? It enables you to import data from a CSV file to a specific content type.

Now, imagine this scenario:

You need to set up a “Customer” Drupal 8 content type, with multiple fields (title, body, discount, customer picture...). And then “inject” data extracted from your CSV source into this content type.

First things first: download and enable the Content Import module.
 

Step 1: Create Your Content Type

For setting up a new content type, just:

Go to “Home”> “Administration” > Structure” > “Content Type” > “Add Content Type”

How to Import Data from a CSV File to Drupal 8: Create a Content Type

Image source: OSTraining.com.

Next, add the relevant fields to your content type:

  • Go to “Manage Fields”
  • Hit the “Add Fields” button

Words of caution:

  • keep in mind to set the “Date” field as “Date and time” 
  • … to set ON/OFF in your “Discount” boolean field to Yes/No (if you do have such a field in your content type, of course)
  • while defining the settings of your image field, remember to configure the file directory for your images as content_type_name/images 
     

Step 2: Get Your Spreadsheet Ready to Be Imported 

Time to prepare your CSV file for the import. Here are a few critical guidelines to consider:

  • your fields' machine names go in the first row
  • enter the values for each field in your content type (as for your images, their file names stand for their values)
  • the “langcode” column is a must-have; make sure it matches the specific langcode of your Drupal 8 website
  • the “title” column is crucial, as well

Note: as for the fields that your Drupal content type can include, they range from Content to Users, to Geolocation, to Date, Timestamp, Entity Reference for Taxonomy, Boolean

Next, just save your file as a Comma Separated Values (CSV) file. 
 

Step 3: Upload Your Images to a “Public” Directory

If there are images involved, as well, here are the additional steps to take:

  • First, download the IMCE module
  • Go to site_name/imce
  • Set Up a folder having your content type's name (or one named after your own specific content type) inside the public directory
  • Then set up an /images sub-folder, as well
  • Next (bulk) upload your images to this last folder using the “Upload” button

How to Import Data from a CSV File to Drupal 8: Upload Images to Public Directory

Image source: Drudesk.com.
 

Step 4: Import Data from a CSV File to Drupal 8

Time has come for you to migrate the content stored in your CSV source.

Here are the key steps to take:

  • Navigate to "Configuration" > "Content Authoring" > "Content Import"
  • Select the due content type in the “Select Content Type” field
  • Hit the “Upload File” button
  • Search for the CSV file 
  • Click “Import”

The instant result? 

The imported items get displayed in “Content”, with all the specific field values migrated from your CSV file.

The END!

And this is how you do it. These are 2 valid approaches to your “CSV import to Drupal 8” challenge.

Which solution would you prefer?


Photo by rawpixel on Unsplash.