CSV To Node importer example
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
This is geared toward people just starting to use feeds. The goal is to provide a simple walk through example. Another great place to see examples are the simpletests in the module itself (feeds/tests). In these tests the feed data is generally in feeds/tests/feeds. The configuration options for that test are found in the *.text field in a class.
In this walkthrough, I'm using the feeds/tests/feeds_mapper_field.test example which uses the tests/feeds/content.csv data. I will basically be walking through the simpletest. It would be helpful if the next person that comes this way adds some screenshots or a quick video of the process.
Code Import CSV to nodes simpletest from feeds/tests/feeds_mapper_field.test
function test() {
// Create content type.
$typename = $this->createContentType(array(), array(
'alpha' => 'text',
'beta' => 'number_integer',
'gamma' => 'number_decimal',
'delta' => 'number_float',
));
// Create and configure importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('content_type' => $typename));
$this->addMappings('csv', array(
array(
'source' => 'title',
'target' => 'title',
),
array(
'source' => 'created',
'target' => 'created',
),
array(
'source' => 'body',
'target' => 'body',
),
array(
'source' => 'alpha',
'target' => 'field_alpha',
),
array(
'source' => 'beta',
'target' => 'field_beta',
),
array(
'source' => 'gamma',
'target' => 'field_gamma',
),
array(
'source' => 'delta',
'target' => 'field_delta',
),
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 2 nodes');
// Check the two imported files.
$this->drupalGet('node/1/edit');
$this->assertNodeFieldValue('alpha', 'Lorem');
$this->assertNodeFieldValue('beta', '42');
$this->assertNodeFieldValue('gamma', '4.20');
$this->assertNodeFieldValue('delta', '3.14159');
$this->drupalGet('node/2/edit');
$this->assertNodeFieldValue('alpha', 'Ut wisi');
$this->assertNodeFieldValue('beta', '32');
$this->assertNodeFieldValue('gamma', '1.20');
$this->assertNodeFieldValue('delta', '5.62951');
}
- Create content type with the following settings:
- content type machine name: feeds_test
- field with machine name "alpha" field of type "text"
- field with machine name 'beta' field of type 'number_integer'
- field with machine name 'gamma' field of type 'number_decimal'
- field with machine name 'delta' field of type 'number_float'
- Create new importer named "Content CSV" with a machine name of "csv" at admin/structure/feeds/create
- Basic Settings (admin/structure/feeds/csv/settings): Use Standalone Form, Periodic Import Off, Import on Submission checked, process in background unchecked
- Fetcher admin/structure/feeds/csv/fetcher: File Upload
- Fetcher Settings admin/structure/feeds/csv/settings/FeedsFileFetcher: leave defaults
- Parser admin/structure/feeds/csv/parser: select CSV Parser
- Parser Settingsadmin/structure/feeds/csv/settings/FeedsSyndicationParser: none
- Processor admin/structure/feeds/csv/processor: set to node processor
- Processor Settings admin/structure/feeds/csv/settings/FeedsNodeProcessor: any settings will do. Content type should be feeds_test or whatever test content type you created.
- Processor Mappings admin/structure/feeds/csv/mapping:
'source' => 'title', 'target' => 'title',
'source' => 'created', 'target' => 'created',
'source' => 'body', 'target' => 'body',
'source' => 'alpha', 'target' => 'field_alpha',
'source' => 'beta', 'target' => 'field_beta',
'source' => 'gamma', 'target' => 'field_gamma',
'source' => 'delta', 'target' => 'field_delta', - Now go to /import and import. You can upload the .csv directly from feeds/tests/feeds/content.csv
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion