How to 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:
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).
- 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
- Set the On and Off labels in your Discount boolean field to Yes and No respectively
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
- Save your spreadsheet as a Comma Separated Values file. Once you do, your file will have a .csv file extension.
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)
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
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!
Can this module also be used for commerce purposes? As a Drupal 8 alternative for commerce feeds?
well Patrick, I just read what commerce feeds does. However I think that’s the main purpose of the module. Both modules refer to database modifications. Commerce feeds works with other types of strucrured data, not just csv
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?
Hello Brent,
sorry I’m not getting your question totally. Could you please be more specific?
Can you please share you csv file ?
Hello Tourist,
you can find the file here: https://gist.github.com/jorgemontoyab/96f21a190c6c690de7228a84059103a3
is title and body mandatory?
Hello agryn,
Only the title field is mandatory in Drupal. The body field is not mandatory. You can configure it as mandatory if you want/need by editing the field, you could import some empty body fields if you want/need or you could even delete the body field if you don’t have body content.
>>> 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?
Hello Emil,
you’re right. The post was edited so it makes more sense now.
Thank you for your remark!
can not use Cyrillic?
Hello Andrey,
I don’t see why it shouldn’t work with cyrillic. Did you specify the langcode in the csv file? Does this langcode match the langcode of your site?
the problem is solved: changed the encoding to UTF-8 =)
happy to hear that!
Thank you for the tutorial. Does this module work when importing content to entities instead of nodes in Drupal 8?
Hello, Steve Le. If you take a look at the project page, you’ll see that you can import all kinds of entities, for example users or taxonomy terms, not just nodes
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.
same issue
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?
Hello Anthony,
I don’t think you can do that with this module since you would have to have some type of primary key to avoid duplicated entries. Instead of that, I think you could filter the duplicated entries prior import in your datasheet program. With an old file and a new one, there has to be some type of way to filter out duplicated entries.
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
Hello KNorton,
that’s all you have to do. Put the machine name of the entity reference field on the first row. Make sure you include also the title field as first field in the row and its langcode, since they’re mandatory. Take a look at the datasheet in Step #2.
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
Hello Kylie,
I replicated the whole scenario and came to the same result as you. Unfortunately there’s some kind of bug (I would call it a bug), which prevents entity references to be imported. There’s a similar question in the issue queue (https://www.drupal.org/project/contentimport/issues/2911302), but there’s no proper answer either. I recommend you as a last suggestion to write on the issue queue thread, maybe you’ll have there a little bit more luck. Sorry about that!
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
Hello Federico,
I think such tasks are beyond the scope of this tutorial and as far as I understand this module won’t be able to do such import. You should take a look at the Migrate API documentation.
Cheers
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.
Hello Steven,
please take a look at step #1. Maybe it’s related to the situation described within the link.
Cheers
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
Hello Rajith,
it’s difficult to know, since the tutorial has worked for everybody else. Make sure all file names are written exactly the same in the csv file. Check the field names also. It helps sometimes to make the whole process again from the beginning. Try to change the image fioles and see what happens.
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.
Hello Robert,
I haven’t tried with a multilingual site, so I won’t be able to help you. Maybe if you try posting your question in the module’s issue queue.
Regards
Thanks for the reply, it is working fine now with the latest 8.x 4-1 version of content import.
Glad to hear that!
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!!!
Hello Josh, I’d post a request in the issue queue, since this behavior is really strange.
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
From the node listing page, you can select all nodes and then choose the save content operation.This re-saves all the nodes.
Hi, thank you for this module. But it is impossible to import in french language. Have you got a solution?
Hello Leroy,
Did you specify the langcode column in the csv file? Does this langcode match the langcode of your site?
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
Those were my guesses. I read something about the site encoding in this comment thread, you could try to set the encoding to utf-8 (I guess inside a template). Maybe if you drop a line in the issue queue.
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.
Hi Vijeth,
thank you for your feedback. Did you check folder permissions? It could be anything, but that would be the first thing I’d check.
Regards
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.
Hello Peter. Thanks for the tip.
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.
Hello Jefferson. There are multiple causes for an error 500. Maybe you should check the Drupal and server logs first. There is more information. Cheers!
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
Hello kattie,
could you explain your problem a little bit more? I’m afraid, I’m not understanding you right.
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.
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
Hello Kattie, maybe your images folder doesn’t have the right permissions. Did you check permissions for the “files” and “customdir” folders (775)?
yes u gave checked ,it does not work for the images but i have found the alternate way for this
how to deal with select box and multiselect box data from csv
Hello Lalit,
I honestly don’t know. Maybe it’s time for you to take a look at the Core Migration tools.
Cheers
and not able to attach the file type field
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.
Hello Rob, sounds like you don’t have enough permissions to access the files folder.
To be honest, I don’t even know if pdf files are supported. The module’s page talks about images only. Maybe if you try with images and not with files and see if it works…
how to import texonomy and terms value from csv file. using contentimport module in drupal 8
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
Hello Christopher,
I have no clue. Maybe you should try to post an issue in the issue queue. Sounds like a compatibility problem.
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.
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.
Very Helpful for me as a beginner. Thank you JORGE
Sure thing Suryam!
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?
need help, how do field mapping for the Address module ( Street address, City, State, Zip code) ?
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.
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.
Hi ,I want to import excel file to drupal 8 for user with feed method. ?
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?
Hi Ben,
have you also tried with single quotes? That is the first thing I would do (I don’t really know though). Some PHP expert could help you further.
Cheers
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.
Hi Srinivas,
I don’t think date range fields are supported.
Cheers
Hi I followed your article.The image field is not migrating.It is showing as empty.Can you please help
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.
Hi jorgemontoya,
How we can import Taxonomy field?
Thanks
sachin