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.
To import your data from a CSV file, you need to install and enable the Content Import module. In this tutorial, we'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|
The name of the customer / this is the default Drupal title field for each content type
|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 with 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.
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-d\Th:i:s');
Replace them with the following two lines:
$dateTimeStamp = strtotime($data[$keyIndex[$fieldNames[$f]]]);
$newDateString = date('Y-m-d\TH:i:s', $dateTimeStamp);
You’ll find more information about this error here.
I hope you enjoyed this tutorial.