Search This Blog

Drupal Tips : How to bulk import data to content type in Drupal?



This post is an example on how to bulk import data to auto create a content type in Drupal. This process is very useful when you need to create a number of content of the same type. In this example were will be using Feeds Module to achieve that.

The example scenario here is that I have created a "student" content type with the fields "Name (as Title), Date of Birth, Registration ID, Gender". Since the process of adding all students will be a repetitive task, it is best if we import.

The content type looks like:

For simplicity, I have only used field type - "text". You may experiment with other field types too. 


Modules required for import:
Download, install and enable the Feeds module just like you would install and enable any other module in Drupal.

Module/Sub-module to enable:
Once the Feed module is installed, enable the following-
  • Feeds
  • Feeds Admin UI

 
Setting up the Feeds importer:
After enabling the Feeds module, it's time to create the importer.
  • Go to Admin > Structure > Feeds Importers
  • Click "Add Importer"
  • Provide a name and a description for this importer
  • Click on "Create"
  • You will come to a page where you need to setup the importer.
  • There are found sections - Basic Settings, Fetcher, Parser and Processor
The following are some settings I chose for this particular example: (see screenshot on the right)

Basic Settings: 

The basic settings defines the general behavior of the importer such as whether it is attached to a content type (meaning when the content type is clicked to add a new content, the import form is attached to it), periodic import settings etc.

Attach to content type : Use standalone form
When this is chosen, the import form is not visible when "Add content" > "Student" is clicked. Instead it is accessible from http://domain/import

 Import on submission: checked

Fetcher Settings:

Fetchers are responsible for loading data such as whether to load a local import file or download content from a URL etc. 

I chose "Upload content from a local file" because I would be uploading a CSV file.

Parser Settings:

Parser settings is for organizing and telling Drupal how to treat the data being imported.

I selected CSV parser here to parse data in Comma Separated Value format.

Processor Settings:

Processors settings is for telling Drupal what to do with the data Imported. Here we define which type of content to be created, whether to replace existing data, etc.

  • I chose "Node Processor" here to create and update nodes from parsed content.
  • Settings:Bundle: Here we define which type of content to create from the imported data.
  • Mapping (important): define which field from the source to be mapped with the fields in the Drupal content type. For example: In my Drupal content type "Students", I have a field "Date of Birth". But in my CSV import file, I would like to mention on DoB. So I need to map DoB to "Date of Birth" etc.
 
  •  After finishing the settings, save the imported.
Preparing the CSV file to import:
You can either prepare the CSV file with headers and define the mapping in processor settings, OR define the fields to map first in the mapping and prepare the CSV file based on it. The latter process seems to be easier and safer to me as there is a template we can download after saving the importer.
  • Go to http://domain/import
  • Click on the name of the importer created
  • A page opens with the importer form
  • In the same page, click on "Download a template". 
  • A CSV file is downloaded with the fields required. This is good to know what the importer is expecting in the field headers.
  • Create the data to import
  • Upload the CSV file.
  • Contents are created.
Tags: 
If you have tags in the content type, enter each tag separated by semicolons in the same cell for each row of data. Example: to include tags "funny","interesting", enter "funny;interesting" in a single cell.

Taxonomies: 
Setup the mapping in the processor settings first. Download the template. Just like tags, make sure taxonomies are separated by semicolons. Example: to include taxonomies "funny","interesting", enter "funny;interesting" in a single cell. The terms should already be existing. Otherwise I experienced that a new term not in the list of taxonomy vocabulary is displayed blank.


Sponsored Links


2 comments:

  1. Thanks for the nice drupal tips. Great helpful. Your collection of this information is Superb.
    web development Melbourne | Magento Development Company

    ReplyDelete
    Replies
    1. Thanks Cado.
      Just jotting down my experiences. Thanks for dropping by!
      Good Day!

      Delete

Related Posts Plugin for WordPress, Blogger...