The Feeds module in Drupal 9 is a powerful tool for importing external data into your Drupal website. It supports a variety of formats, including CSV, XML, and JSON, and can import content into nodes, taxonomies, and other entities in your Drupal site. To enhance the importing process, you can use the Feeds Tamper module, which allows you to modify data before it is saved. This tutorial will walk you through the process of using the Feeds module along with Feeds Tamper to import data from a CSV file.
- Required modules:
- Install and enable the following modules:
- Create a content type:
- If you don’t already have one, create a content type for the data you want to import from the CSV file.
- Set up a Feeds Importer:
- Go to
Structure > Feeds types
- Click on
Add feed type
- Enter a name and description for your importer
- Go to
- Configure the Fetcher:
- Click on the “Fetcher” dropdown, and choose how you’d like to fetch the file. For this tutorial, use
Upload File
to manually upload the CSV file. Refer to the Fetcher a. and b. images below.
- Click on the “Fetcher” dropdown, and choose how you’d like to fetch the file. For this tutorial, use
- Configure the Parser:
- Click on the “Parser” dropdown, choose
CSV
, and configure the appropriate CSV settings, like delimiter, No headers. The default settings will work for most of the use cases. Refer to the images below.
- Click on the “Parser” dropdown, choose
- Configure the Processor: Click on the “Processor” dropdown and choose Node. Then, click on the “Content Type” dropdown, and choose the target content type, and configure additional settings like author and default language. The Processor dropdown allows you to choose the type of entity that will be created when importing data. Refer to the images below.
Language: This setting allows you to select the language for the imported content.
Update existing content items: This setting determines how existing content items are handled when new data is imported. You can choose to update existing content items, not update them, or replace them entirely. The module will use the unique mappings you have set up to determine which content items are considered “existing” and should be updated or replaced.
Previously imported items: This setting allows you to choose what to do with items that were previously imported but are no longer present in the feed. You can choose to keep them or take some other action.
Expire content items: This setting allows you to specify when content items should be deleted. You can choose to never delete them or set a specific time period after which they will be deleted.
Owner: This setting allows you to specify the owner of the imported content. You can choose to use the feed author as the owner or select a specific user. If you leave this field blank, the owner will be set as Anonymous.
- Click on Save and add mappings
- Map the source CSV fields to the target content type fields:
- Click on the Mapping tab: This will take you to the mapping configuration page where you can map source data to target fields.
- Map source CSV columns to the target content type fields by clicking Select a target: This will allow you to select the target Drupal field that you want to map data to. Then, in the “Source” column, select New CSV Source and enter the name of the column from your CSV file that contains the data you want to map to the selected target field.
- Optionally, configure unique fields to avoid duplicate imports: This allows you to specify which fields should be used to determine if an imported item is a duplicate of an existing item. If a duplicate is found, the module can be configured to update or replace the existing item.
- Tweak other configurations like language handling: This allows you to configure additional settings for your importer, such as how language should be handled.
- Click Save: This will save your mapping and other configuration settings.
- Import the data using the Feeds importer:
- Go to
Content > Feeds importers
- Click on
Import
for the importer that you created previously - Upload your CSV file (If you configured file upload as your fetcher)
- Click
Import
- The imported content should now appear as nodes of the target content type within your Drupal site
/admin/content
- Go to
Use Feeds Tamper to manipulate imported data
The Feeds Tamper module is a contrib module for the Drupal Feeds module that allows you to modify data before it is imported. It provides a user interface for adding “tamper” plugins to the individual mapped fields of your Feeds importer, which can perform various data manipulation tasks such as splitting, combining, or rewriting data.
- Confirm that the Feeds Tamper module is enabled.
- Go to the configuration page for your feed importer: Navigate to the configuration page for the feed importer that you want to use with the Feeds Tamper module. You can do this by going to the “Structure” page, then clicking on “Feeds” and selecting the importer you want to configure.
- Click on the “Tamper” tab: On the configuration page for your feed importer, click on the “Tamper” tab to access the tamper settings.
- Add tamper plugins to mapped fields: In the tamper settings table, find the mapped field to which you want to add a tamper plugin to. In the “Plugin” column for that field, click on the “Add plugin” button. Select the plugin you want to use from the list of available plugins, then configure its settings as needed.
- Save your changes: Once you have added and configured all of the tamper plugins you want to use, click on the “Save” button to save your changes.
- Repeat these steps for all the source columns you want to modify before storing the data
Feeds tamper module’s use cases
- Data cleanup: You can use the Feeds Tamper module to clean up data before it is imported. For example, you can use the “Find and replace” plugin to replace specific words or phrases in your data.
- Data transformation: You can use the Feeds Tamper module to transform data before it is imported. For example, you can use the “Explode” plugin to split a single field into multiple values, or the “Implode” plugin to combine multiple fields into a single value. The “Explode” plugin is useful when mapping multiple comma-separated fields from the CSV file to a taxonomy reference field.
- Data filtering: You can use the Feeds Tamper module to filter data before it is imported. For example, you can use the “Keyword Filter” plugin to only import items that contain specific keywords.
- Data rewriting: You can use the Feeds Tamper module to rewrite data before it is imported. For example, you can use the “Rewrite” plugin to change the format of dates or numbers in your data, or the “Pathauto” plugin to automatically generate URL aliases for imported content.
And that’s it! You have successfully configured the Feeds module in Drupal 9 to import and manipulate data from a CSV file using the Feeds Tamper module.