How to Import Product or Blog Post Data into Drupal from CSV Files? Step by Step
In this article, I'll show you how to import data from CSV files into Drupal. This is often a step in creating a new website, and the solutions shown here will come in handy when transferring data from an old system to a new one. I’ll also demonstrate how to handle such a situation using the Feeds module and its Feeds Tamper extension and walk you through importing data using three types of data as examples: products, users, and blog posts.
You'll see how easy it is to move content into Drupal by mapping the data accordingly and how to manipulate the data with the available modules to customize it to your needs easily.
I encourage you to read the article or watch the episode of the “Nowoczesny Drupal” series (the video is in Polish), where I show you all the steps one by one.
Drupal Feeds module - what is it and how does it work?
The Feeds module allows us to import data from various formats, such as CSV, JSON, XML, and RSS. It lets us easily move large amounts of data into Drupal, which is especially useful when migrating from older systems or integrating Drupal with external data sources.
In this post, we’ll also use the Feeds Tamper module, which extends Feeds' capabilities by allowing us to manipulate the data we import into Drupal. For example, we can split fields, remove unnecessary characters, and modify data structures, giving us complete control over how the data will end up in the system. Feeds Tamper is a key tool when data is inconsistent or needs additional processing before it can be imported.
Together, these two modules - Feeds and Feeds Tamper - create a very flexible and robust solution that makes it easy to work with data in Drupal.
These modules have already been installed on my system, so let's proceed with a step-by-step demo of the import process and how to use them.
Importing products from a CSV file
We have our sample CSV file, which contains columns with the following data:
- Title - product name,
- Category - product category,
- Description - product description,
- Tags - tags related to the product,
- Photos - ID of photos that we previously imported into Drupal media.
Now, let's move on to Drupal. I'll show examples of the Droopler distribution (it already has content types such as "Product," which has a title, image, content, categories, and tags).
If we have the Feeds module installed, we can find Feeds Type in the menu in the structure and add new Feeds for ourselves.
Configuring the Drupal Feeds module
- We go to the "Structure" tab and select "Feeds Type."
- We add a new type, "Feeds," and name it "Products."
- We set up Fetcher, where we select "Data Source" - "CSV" and specify the type of data we want to import, that is, "Node" and content type "Product."
- We configure the Parser so that the data is separated by a comma.
- We set the Processor, which will update the existing data the next time the file is imported.
We save the changes and proceed to mapping.
Field mapping for products
A crucial part of the import is mapping fields from the CSV file to corresponding fields in Drupal. This process involves assigning data from CSV columns to specific fields in the content type, such as title, category, content, tags, or images.
Here, we have a default list of fields, which the module provides us with, from a given content type for a product. Let's select "Title."
Next, we select the source, i.e., a column from the CSV with the name "title." We select that it’s unique so that we can recognize and update products by it later (in the future, we can change this to ID or product ID).
Then, we select the "Category" field and assign a column with the category name. We can immediately use the field with a reference. Here, I’ll show the example of "name."
Next, we can check the Autocreate option to have Drupal automatically create new categories if they don’t exist in the system.
The following steps are to set tags, media, and descriptions:
- Tags: we assign the "Tags" field to the tags column in the CSV file.
If the tags are separated by a semicolon, we can use the Feeds Tamper module to split them into separate entries.
- Photos: we assign the "Photos" field to the corresponding media IDs in Drupal.
It’s important to import photos into the system beforehand because the Drupal Feeds module assigns photos based on their ID.
- Description: we assign the "Description" field to the corresponding column containing the content of the product description.
Importing data
Once we’ve finished mapping the fields, we can proceed to import the data.
- We go to the "Content" tab -> "Feeds."
- We click "Add a new channel" and select the Feeds type "Products."
- We select the CSV file with the product data and click "Import."
Once it's done, Drupal will let us know the number of imported products. In this example, we received information that two products were imported.
If tags or other data have been imported incorrectly, we can use the Feeds Tamper module to manipulate the data - for example, to separate tags based on the separator.
Feeds Tamper - when is it worth using?
In some cases, data requires additional processing to be imported correctly into Drupal. This is where the Feeds Tamper module becomes essential, as it allows you to modify the data before importing it.
To use Feeds Tamper, we go to the "Structure" tab, then select our Feeds type, for example, "Products." Here, we can add various data manipulation options for individual fields. Feeds Tamper offers many tools, such as the use of regular expressions (regex) or the explode function, which allows us to separate data based on a specific separator.
For example, for the "Tags" field, which is separated by a semicolon in the CSV file, we can add an explode operation to separate the tags into separate entries. For the "Photos" field, where photos are separated by a comma, we set the appropriate data manipulation so that Drupal can correctly recognize each photo. After applying these operations, we return to the import process and continue it, being sure that the data will be correctly imported and assigned to the appropriate fields.
Manipulating data in this way is extremely useful when the data is complex or needs to be transformed to fit Drupal's structure.
Importing users from a CSV file
Now, let's move on to importing users. Our CSV file contains data such as:
- E-mail - the user's e-mail address,
- Username - user login,
- Role - the role assigned to the user,
- Status - whether the user is active or not in the CMS.
We go once again to the "Structure" tab - > "Feeds Type" and add a new import type named "Users." We choose "Data Source - CSV" and data type "Users." In the processor settings, we select "Update existing users" to avoid duplicates.
We’ll be updating existing users, so we’re moving on to mapping.
Field mapping for users
When importing users, we have fewer fields to map:
- Email: we assign an e-mail column to the "E-mail" field in Drupal. This field must be unique because each user must have a unique email address.
- User name: we assign the user name to the corresponding column.
- Role: we assign a role to the user role column. We can choose whether we want to assign the role based on label or ID.
- Status: we assign a status that determines whether the user is active or blocked.
At this point, we have two "Feed Types": "product" and "users." We’ll take care of importing users.
Importing users
We go to the "Content" tab -> "Feeds." We add a new channel for users, select the CSV file and click "Import."
Once the process is complete, we check that users have been correctly added with the correct roles and statuses.
As you can see, importing users into the Content Management System is simple and can be done quickly.
Importing blog posts from a CSV file
The last example is importing blog content. As with products and users, we use a CSV file containing the following fields:
- Title - we assign the title of the blog,
- Teaser - we introduce a summary of the content,
- Content - we assign content to the corresponding paragraph in Drupal (full HTML),
- Photos - we map photos based on the ID from the CSV,
Category - add a category based on the name, and the system will automatically create new categories if they don’t exist.
Configure the Drupal Feeds module for blog posts
Under "Structure" -> "Feeds Type" we add a new import type, which we’ll call "Blog posts."
This is an import from a CSV file into a node that will fall into blog posts.
As in the case of products and users, we assign ourselves these fields, then set the data and select the blog category. Next, we refer by name and new categories are created.
Save. Then we go to create a new Feed, a new channel. We already have the blog posts.
At the very end, we select a file for ourselves and import it. When the import is complete, we see that the blog posts are correctly imported, with correctly assigned categories and images.
All items are displayed correctly; we can also see in the listing that everything looks nice.
Importing data into Drupal - summary
The Feeds module and the Feeds Tamper extension are comprehensive tools that make it easy to manage large amounts of data. With them, we can automate the process of migrating and importing content, which saves a lot of time and work. Whether we are importing products, users, or blog posts, we can customize the process by mapping data from a CSV file to the appropriate fields in Drupal.
Key steps in the import process:
- Configuring the Feeds module: specify the data source and content type and set the parser and processor to process the data as required.
- Field mapping: assigning columns from the CSV file to the corresponding fields in Drupal, which allows for an accurate representation of the data structure.
- Data manipulation with Feeds Tamper: the ability to transform data before import (e.g., separating tags or handling multimedia), which is extremely useful for more complex data formats.
It's worth remembering that the Feeds Tamper module allows additional control over imported data, which is especially useful when the data needs to be adjusted - for example, when tags are stored in a single field or images are separated by commas.
If you’re planning to migrate your data to Drupal or manage a large amount of content, the Feeds module and its extension are indispensable tools that will allow you to automate many processes, avoid errors, and save valuable time. What's more, the ability to update existing data makes the process efficient and flexible. Do you need further assistance with these processes? Our Drupal agency experts can help you optimize your work with this CMS.