Drupal 8 - Importing CSV Data with Feeds

By peterm, 22 May, 2018

For the project, http://building-addresses.ucsc.edu, I decided to combine a few training opportunities for myself into an easier to support system. As I prepare to retire, I'm going back to production systems and getting them off AngularJS/MongoDB and into Drupal which will make for easier maintenance and longer life span.

After getting Docker4Drupal running and configured, I began looking at how we could take the existing CSV data that has building information and get it ported into Drupal 8.

Creating a building content type. Add the text fields needed. I did use decimal fields for the latitude, longitude and some multi-value fields for abbreviated names and additional names fields.

Then we have to install the Feeds module and create a Feeds type to import the building information from the CSV. Here are the steps.

Starting with our CSV file, let's see the header (column names) in the first row. As you can see, I've camel cased names like assetName. 

We want to create a Content Type and name it Buildings. We'll add each of the field names from the CSV as text fields in Drupal. Latitude, Longitude can be decimal integer format. I typically try to keep the order of the columns in the CSV and the content type ordered in the same way to reduce eye strain and general confusion.

Once our content type is completed, we can do a test entry to make sure all the display info is working to your requirements. Then we'll move on to the next step.

Image

Adding a feed type. In this step, we have navigated to Structure > Feed Types > Add Feed Type.

Basically, we care creating and configuring the attributes of how we want to pull in data with this feed. We'll give it a name, description and other settings. Here you will note that I'm going to Upload and use the CSV parser on the Building content type.  

Image

After the feed type is configured, we move on to setup how the data coming from the uploaded CSV will map to the fields int he Building content type. Our target in the content type will be matched to a source from the CSV. Note the difference in naming conventions on Asset Name and assetName.

Image

Below is the finished field mapping. Note the use of Title mapping to assetName. Drupal needs to have a Title field, so its got some duplication.

Image

Here is where I got stuck and decided to write up this documentation. I couldn't figure out how to get the feed type to act as an importer. It had been a few years since I'd worked with Feeds. 

Navigating to Content > Feeds > Add Feed, you will need to get a name to the importer, then locate the CSV file we're gong to upload. From there we save and import. 

Image

While we're brining in the records we get a progress indicator.

Image

And finally, we get a success message.

Image

Next, we will start figuring out the theming for this project and integrating the other modules we'll need to recreate our existing app.

 

Tags