Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to import data from a CSV file to Drupal 8

Parent Feed: 

There is often a need to import and export content to and from your Drupal 8 website. The ability to do that lets you use the power of content in Drupal 8 more fully. One of the most popular content sources is a CSV file. Drupal 8 has a number of useful contributed modules for getting data from it, and many cases also need customization. Our Drupal team provides smooth CSV import for even for the most complex cases. Today we would like to share a simple way to import data from a CSV file to Drupal 8 with you.

For a start, what is CSV?

CSV, which stands for comma-separated values, is a popular format for data storage. A CSV file stores data as a table where values are separated by commas. CSV is compatible with any spreadsheet programs such as Google Spreadsheets, Microsoft Excel, and so on. Each value uses a cell in the table.

CSV files are incredibly lightweight, easy to organize, and awesome for manipulating large amounts of data. Huge tables with data can be quickly edited by website administrators and sent to Drupal when ready.

Some ways to import data from a CSV file to Drupal 8

Among the popular solutions for CSV import to Drupal 8 websites, there are contributed modules like Migrate Source CSV, Migrate Plus, and Migrate Tools. The CSV import process with these modules includes the creation of the migration configuration and the use of the “migrate-import” Drush command.

And there also is one very simple solution called the Content Import contributed module, which we would like to show in action.

An example of CSV import via the Content Import module

The Content Import module lets website administrators import data from a CSV file to a specific Drupal 8 content type. A content type has fields, and a table has columns, and this is where they should match. Remember, we can always save a CSV file as a table.

Each CSV file column should list the values of a particular field of the content type. The first cell of each column should match the field’s machine name.

Some special supported Drupal fields include:

  • Entity Reference for Taxonomy, Users, and Content
  • Geolocation, Timestamp, Boolean, and Date

There also are two columns announced as mandatory.

  • The first column should be called “title.”
  • There is also a “langcode” column that does not match a specific field of the content type. The default is “en” for English, and you can use other language codes.

In our example below, we are not using the langcode column, and still everything works. So it looks like using only one of these two is mandatory.

If your data has images, there will be a little more hassle — you will need an additional IMCE module for uploading images into the “public” folder.

Of course, our example will include images! ;) So let’s create a sample Drupal 8 content type and import CSV data into it. We have a tasty content type on our Drupal 8 website — “Menu item.” It has the following fields.

Drupal content type fields

In the settings of the image field, we specify the directory to which we will upload images like this: [content_type_name/images].

 Specifying the image upload directory in the field settings

The “Available for delivery” field is of “Boolean” type, and we put the options as “Yes” or “No.”

The Boolean type field in Drupal

Then we download the IMCE module that will help us put images in this directory. We go to site_name/imce, open the “public” directory, create the folder with our content type name, and within it, create the folder “images” so that the path is public/content_type_name/images. Then we upload our images there. A bulk upload works well for this.

 Uploding images with IMCE

Then let’s prepare our CSV file. We can create it as a Spreadsheet first, so it’s more handy to work with the cells. We put the machine names of the fields into the first row. Then we fill in the values for each field. The values for the images are their file names.

Data as a spreadsheet

When the spreadsheet is ready, we download the file as CSV.

Data as CSV

It’s time to import the CSV data to the Drupal 8 website. With the Content Import module installed and enabled, we go to Configuration — Content Authoring — Content Import and select the content type to import the CSV data to.

When we do so, we also see sample CSV file available for download. We are not interested in the sample right now, so let’s choose our CSV file from the computer and click “Import.”

Content import from CSV to Drupal 8

In a few seconds, we see the content is imported. All items are listed in Content, and each of them has all the field values imported from our CSV.

 Drupal content listedDrupal content item with fields

Our menu deserves to be created as a view on our Drupal 8 website, so it represents all our items in an attractive way.

Drupal content in a view

Your perfect CSV import

We have shown you how to import data from a CSV file to Drupal 8. Please keep in mind that this simple example will not fit on just any website. Especially if you have plenty of customizations, or need complex mapping between the CSV columns and Drupal fields, you will need custom migration solutions.

Contact our Drupal experts — and we will advise you the optimal CSV import scenario. And, of course, they will implement everything for you. Get the best from the efficient data formats and smart data exchange!

Author: 
Original Post: