How to Import Data from CSV Files to Drupal with Content Import

Import Data from CSV Files to Drupal with Content Import

Sometimes you may need to import data from a CSV file into Drupal.

We’ve spoken with OSTraining users who need to import from another CMS, and uses who need to import from a business spreadsheet.

There is no easy way do this import using the Drupal 8 core. To import your data from a CSV file, you need to install and enable the Content Import module.

In this tutorial, I’ll walk you through the process of importing data with Content Import.

In this example, we’ll import data into a “Customer” content type. This Customer content type will have the following five fields:

Field Name Field Description Field Type
Title The name of the customer / this is the default Drupal title field for each content type Plain text
Body Customer information Long text
Contract Date The date when the contract with the customer was signed In the mm/dd/yy format
Customer Picture The picture of the customer Image
Discount This field indicates if the customer qualifies for an extra discount at the end of the year Boolean (Yes/No)

Step #1. Create the Customer content type

After creating the Customer content type and adding fields, you will have the following starting point:

Starting point

There are some details that you have to take into account to run this process without complications:

  • The date field has to be set as Date and time (this is related to the Unix timestamp. You can read more about this here).

Setting the Date Type

  • When creating the Customer Picture image field, configure the file directory for the images as [MACHINENAMEOFYOURCONTENTTYPE/images] You’ll upload your images to this directory with the help of the IMCE module or some kind of FTP software

Configure the file directory for the images

  • Set the On and Off labels in your Discount boolean field to Yes and No respectively

Set the On and Off labels


Step #2. Prepare your spreadsheet for import

You can use the spreadsheet application of your liking for this. I’m using Google Spreadsheets. The langcode column is mandatory

Importing to Drupal from Google Spreadsheets

  • Save your spreadsheet as a Comma Separated Values file. Once you do, your file will have a .csv file extension.

Save file in CSV format


Step #3. Upload the profile pictures to the specified directory

With the IMCE module:

  • Go to yoursite/imce in order to open the IMCE browser
  • Create the /customer folder inside the public directory
  • Create the /images sub-folder inside the /customer folder
  • Upload the profile pictures to this folder by clicking the Upload button on the top (/customer/images folder in our case)

New folder

Upload button

Uploaded profile pictures


Step #4. Import your content from the CSV file

  • Click Configuration > Content Authoring > Content Import

You will see a screen with two options.

  • Select the Customer option for the Select Content Type
  • Click on the Upload File button
  • Select the CSV file on your hard drive
  • Click Import

Click Import

Congratulations! If you followed along with my instructions, you should now see the Content screen of your Drupal installation with your newly imported content.


Troubleshooting Your Drupal CSV Import

While importing content, you may run into the following error on the white page:

“The website encountered an unexpected error. Please try again later.”

To deal with this error, please do the following:

  • In your Drupal site root go to modules > contentimport > src > form
  • Open the contentImport.php file in your text or code editor
  • Find the following two lines (around line 275):

$dateTime = DateTime::createFromFormat('Y-m-d h:i:s', $data[$keyIndex[$fieldNames[$f]]]);
$newDateString = $dateTime->format('Y-m-dTh:i:s');

Replace them with the following two lines:

$dateTimeStamp = strtotime($data[$keyIndex[$fieldNames[$f]]]);
$newDateString = date('Y-m-dTH:i:s', $dateTimeStamp);

You’ll find more information about this error here.

I hope you enjoyed this tutorial.

If you want to learn more Drupal, join OSTraining now. You’ll get access to a vast library of Drupal training videos, plus the best-selling”Drupal 8 Explained” book!

Author

  • Jorge Montoya

    Jorge lived in Ecuador and Germany. Now he is back to his homeland Colombia. He spends his time translating from English and German to Spanish. He enjoys playing with Drupal and other Open Source Content Management Systems and technologies.

    View all posts
5 3 votes
Article Rating
Subscribe
Notify of
guest

79 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Parick
Parick
6 years ago

Can this module also be used for commerce purposes? As a Drupal 8 alternative for commerce feeds?

crossbow
6 years ago

When I import the CSV file it does not go to field in my content type instead the file goes to files, when I click on files I can see all of the files I tried to import. How to I fix this?

Tourist
Tourist
6 years ago

Can you please share you csv file ?

agryn
agryn
6 years ago

is title and body mandatory?

Emil Johnsson
Emil Johnsson
6 years ago

>>> Sometimes you would like to import a huge volume of data from a CSV file into Drupal. Maybe from another CMS. Maybe from a spreadsheet. But there is no such functionality in the Drupal 8 core.

Wouldn’t Migrate that is now in Core be able to do all of this?

Andrey
Andrey
6 years ago

can not use Cyrillic?

Andrey
Andrey
6 years ago
Reply to  Jorge Montoya

the problem is solved: changed the encoding to UTF-8 =)

Steve Le
Steve Le
6 years ago

Thank you for the tutorial. Does this module work when importing content to entities instead of nodes in Drupal 8?

Namisha
Namisha
4 years ago
Reply to  Jorge Montoya

Hello, I want to import data into a content type which has Entity references as a field into it. When I import the csv file, the Entity reference data do not get stored into Content type but it gets added in the taxonomy. I want it to get stored into the Content type field as well.

Ragu
Ragu
2 years ago
Reply to  Namisha

same issue 

StClairCollege
6 years ago

Hi Patrick,
Say I have a rather large CSV file that has 10,000+ listings of courses that I want to import once or twice a year. If I do this the first time, it works, no problem. But say there have been some changes here and there and in 6 months I need to upload the complete CSV again. Because again, there are 10,000+ listings, I’m not going to know which ones have been updated/added, I just go and import the whole file again. Instead of ignoring the ones that have no changes and just updating changed ones/new ones, it creates all the listings again, so now I have two copies of every course. Is there a way that this module can know that only certain ones have been changed/updated and only update those/import the new ones?

knorton
6 years ago

Hi great blog thank you. Having terrible trouble importing entity reference though. fine with taxonomy. Could you please advise column head (just the machine name in the main content type?) and format for entry. Have tried every combination. Thanks

knorton
6 years ago
Reply to  Jorge Montoya

Hi very sorry but it still is not working. May I send you screen shots as obviously missing something. Works fine for everything else (includes title and langcode) but not an entity reference content type machine name field_artist entered as title as shown under content types > Publications. Label is Artist (the title). Artists set up and can be manually added to fields after import. Tried columns using ID, Artist: Artist Name, or just name, or title, but nothing. The field is auto complete but another field using taxonomy is auto complete and works fine. Sorry to bother you. K

Federico
Federico
6 years ago

Hi Jorge, is there any way to set the following:
1- Creation timestamp of the node
2- The user who created
3- How do you import a translation of a node, knowing that in Drupal 8 they are the same NID

Thanks in advance

Steven
Steven
6 years ago

How to set up the date properly in the csv? Tried many things but still got a wrong date in drupal. I get a date like: 01/01/1970
i set up the date like yy/mm/dd, i tried: yy-mm-dd and yyyy/mm/dd, dd/mm/yy,… Nothing worked. Please help.

Rajith
Rajith
6 years ago

I get this error The website encountered an unexpected error. Please try again later.

title body field_customer_picture field_discount langcode
filp wee p1.jpg on en
sds ee p2.jpg off en
sds red p3.jpg on en

only 1 node with picture was found in the content.

When I removed the all the jpg file name from the list, it works. Any solutions

Robert
Robert
6 years ago

Hello everyone. First of all, I would like to thank those who have contributed to the development of this content import module and to Jorge Montoy for the step-by-step description of how this works.

Does anyone have experience with the import module and a drupal multilingual site? I’m experiencing two issues with importing. My Drupal site is multilingual (NL – default, EN).

First issue.
When importing the CSV file, the content is created in drupal in langcode ‘EN’. Despite the fact that the langcode ‘NL’ is included on each row in the CSV file.

Second issue.
Each import line in the CSV file contains field information for specifically the NL or EN node in drupal. For example, field_description: nl and field_description: en. How can I import this for the same node.

Hope someone can help me further.

Rajith
Rajith
6 years ago

Thanks for the reply, it is working fine now with the latest 8.x 4-1 version of content import.

Josh
Josh
6 years ago

After I import my csv file the module takes me to the content page and displays all the new content. If I click on a link to one of the new nodes I’m presented with “The website encountered an unexpected error. Please try again later.”

Clicking the edit link to review the fields reveal that all the data was imported properly. If I click save and try the link again IT WORKS! I have thousands of nodes to upload so this workaround isn’t a real solution. Is there a was to run some kind of batch command to re-save all my nodes? Is there some other solution I’ve overlooked?

I’ve tried all the fixes including editing the contentImport.php but I think that’s been fixed in the latest version already. I’m running the most recent version of everything and have even completely restarted from scratch several times on different servers. Any help would be much appreciated!!!

Josh
Josh
6 years ago
Reply to  Josh

Thanks for the suggestion. I posted about this issue over two days ago but haven’t had a reply on drupal.org. I wanted to include a little more info here. My csv file looks like this. Is the issue that I’m trying to import a link field, maybe?

title,body,field_date,field_tags,field_audio_link,langcode
James 01/07/18,,01/07/18 08:00:00,tags: James,/sites/default/files/audio/james/James 01-07-2018.mp3,en

doncheks
6 years ago
Reply to  Josh

From the node listing page, you can select all nodes and then choose the save content operation.This re-saves all the nodes.

Leroy
Leroy
6 years ago

Hi, thank you for this module. But it is impossible to import in french language. Have you got a solution?

Leroy
Leroy
6 years ago
Reply to  Jorge Montoya

of course; here is a few lines my csv file:
French is the language of my website

title,field_adresse,field_ville,field_code_postal,field_email,field_site_internet,field_portable,field_image,field_tel_fixe,body,langcode
ROYAL KEBAB,Ctre Commercial Cascade,LAXOU,54520,,,,,,,fr
ROYAL KEBAB,Rue D’ Amiens,ROUEN,76000,,,,,,,fr
ROYAL KEBAB,Rue Du 14 Juillet,CAYENNE,97300,,,,,,,fr
ROYAL KEBAB,Rue Jean Moulin,TERRASSON LAVILLEDIEU,24120,,,,,,,fr
ROYAL KEBAB,1 Place Haute Du Chai,ST BRIEUC,22000,,,,,,,fr
ROYAL KEBAB,64 Rue Montesquieu,AGEN,47000,,,,,,,fr
ROYAL KEBAB 2,6 Place Marcel Pagnol,BRUZ,35170,,,,,,,fr

Vijeth
Vijeth
6 years ago

Hi Jorge, it’s a great blog. I followed your steps for uploading images using IMCE, but the images are not uploaded to Drupal Backend i.e., the field which I have created for images is not showing anything. Please do help me.

Peter P.
Peter P.
6 years ago

I think that if you use Excel and Macintosh line endings might be an issue. The module was silently failing to do anything (upload the file, but no action and nothing in the log file). When I changed the line endings from “Classic Mac” (CR) to Unix (LF), the import worked. I did get the message “the website encountered an unexpected error, please try again later”, but the information seems to be successfully imported.

Jefferson Costa
Jefferson Costa
6 years ago

Hello Jorge! I need import a file with 3k records, but my server just import 370 records and return a error 500. Can you help me? I’m using drupal 8.

kattie
kattie
6 years ago

facing “The website encountered an unexpected error. Please try again late issue,when i check the content import fill no code found as you mention in artical in new version

kattie
kattie
6 years ago
Reply to  Jorge Montoya

Open the contentImport.php file in your text or code editor
Find the following two lines (around line 275):

ContentImport.php file has been update in the new version
you have to look this .btw i have solve this problem ,but unable to show the images
In database images name storing and in folder has images with same name but images not showing.

kattie
kattie
6 years ago

I am unable to show images , i have created folder and set the path and put images in it and name fill in the csv sheet .After upload images not showing . its empty

kattie
kattie
6 years ago
Reply to  Jorge Montoya

yes u gave checked ,it does not work for the images but i have found the alternate way for this

Lalit
Lalit
6 years ago

how to deal with select box and multiselect box data from csv

lalit
lalit
6 years ago
Reply to  Jorge Montoya

and not able to attach the file type field

Rob
Rob
6 years ago

Hello Jorge. I named the file upload field for pdfs, “Attachment”. Everything else in my csv imports except for the attachment titles (field_attachment), given that I also uploaded the corresponding documents to their designated directory. Any tips you can provide will be much appreciated.

MK-Ramdhari
MK-Ramdhari
6 years ago

how to import texonomy and terms value from csv file. using contentimport module in drupal 8

Christopher Lowden
Christopher Lowden
6 years ago

Hello Jorge
Thank you for great page.
I have followed your page to the letter but I too have The website encountered an unexpected error. I am on drupal 8.5.6, the contentImport.php file is as you specified, contentimport-8.x-4.1 is installed and all the folders and files have 777:www-data permissions. The csv is made in libreoffice ubuntu on utf-8 but the file has no header metadata when saved. The csv looks clean. The contentimport.log is always empty and I don’t know where else to look to find out where it the import is struggling. Would you have any ideas where I can look? Many thanks

Christopher Lowden
Christopher Lowden
6 years ago
Reply to  Jorge Montoya

Jorge
Thanks for the reply.
For newbies like me, I advice to read the system logs
https://www.drupal.org/docs/8/core/modules/dblog/overview
as there is lots of info on what the content import is doing. It seems I have an issue with the TAGs that are not being parsed correctly. When I take the tags out, everything works except the images but there is no error for the images not uploading. The instructions on the images folder placement is confusing.

Christopher Lowden
Christopher Lowden
6 years ago
Reply to  Jorge Montoya

Adding to my previous reply, the images were not showing up and until I added and alt image in the image field menu. Once that was setup, my test imports worked. I thought I was home free … but not. I have 4000 images into import. The process now finishes but about half the nodes have the wrong images. I have spend the last few hours cleaning up all I can think of in the csv, I have broken the csv into 300 node chunks, but the errors are still there. I cannot see any logic to it as it looks quite random. Would have any any thoughts. Thanks again.

Suryam
Suryam
5 years ago

Very Helpful for me as a beginner. Thank you JORGE

o4tuna
5 years ago

The listing for this module at drupal.org says that this module affords an *administrative user* the ability to import CSV-formatted data as content. (Cool!)

Is it, however, impossible for other types of users to import content? For example . . . 

Suppose you are creating an “online mall”, in which your users are merchants. The merchants each have stores. It wouldn’t be a good idea to bestow administrator-level user privileges to them. (Their access domain should be restricted to their individual stores.) What they must do, though, is add products to their store. This would best be done by importing a CSV file.

Is the Content Import module a good fit for this use case?

brody
brody
5 years ago

need help, how do field mapping for the Address module ( Street address, City, State, Zip code) ?

vunda
5 years ago

this module still not working. I was using the module Content Import for D8. It works but not 100%. After uploading 90 records stop working. 

Brian Spurlock
Brian Spurlock
5 years ago

Is there a way to import an address field values from the address module?  When I download the sample csv file it only shows one column for the address field.  Not sure how to code that field so it imports into the different address field values. The fields in the csv file that come from the content type are:  title, langcode, field_address, field_map. 

ewen
ewen
5 years ago

Hi ,I want to import excel file to drupal 8 for user with  feed method. ?

Ben Harker
Ben Harker
5 years ago

I am trying to add some taxonomy terms in my import that have colons in the title, which conflicts with the colon used to define the taxonomy library. I have tried putting the term’s name in quotations marks, but no luck. Is there a way that I can encode the colon so it doesn’t cause issues?

Srinivas
Srinivas
5 years ago

I am using the date range (start & end date) in my content type. I am trying to import the content using csv but the date range field is giving me some error. could you let me know the formate for the date range in CSV? I tried adding “-” between start and end date but no luck.

Stanley
Stanley
3 years ago

Hi I followed your article.The image field is not migrating.It is showing as empty.Can you please help

Rune Haukås
Rune Haukås
3 years ago

Hi, Apologies if this is a stupid question, but is it possible to have html code in the csv file? I’m looking to import node information where the body text is already formatted, but I can’t figure out how to get that html formatting with me to the new site.

sharma.sachin013@gmail.com

Hi jorgemontoya,

How we can import Taxonomy field?

Thanks
sachin

79
0
Would love your thoughts, please comment.x
()
x