CSV files often contain a header row with fieldnames. Attached a patch that adds support for this. It does not count and process the first row if you specify in your migration class that there is a header row.
To use in your migration class, change from:
// Create a MigrateSource object, which manages retrieving the input data.
$this->source = new MigrateSourceCSV($this->csv_file(), $this->csv_columns(), array('delimiter' => "\t", ), $this->fields());
to:
// Create a MigrateSource object, which manages retrieving the input data.
$this->source = new MigrateSourceCSV($this->csv_file(), $this->csv_columns(), array('delimiter' => "\t", ), $this->fields(), $this->csv_with_header_row());
and add:
function csv_with_header_row() {
//return TRUE if first row of csv file
return TRUE;
}
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | csv_header_support.patch | 2.22 KB | wouter99999 |
Comments
Comment #1
mikeryanSounds promising, but there's no attachment - please try uploading again.
Thanks.
Comment #2
wouter99999 commentedOops! Attached this time...
Comment #3
stella commentedI couldn't get this to work. It still processed the header row of my csv file and in addition didn't adjust the number of records available to import when running "drush ms"
Comment #4
wouter99999 commentedHi Stella,
strange... Could you open plugins/sources/csv.inc to see if the patch is correctly applied? did you add the extra value to your new MigrateSourceCSV() ? Note that the default is FALSE, so if you did not add the extra TRUE value, it will still take the first row.
Comment #5
mikeryanThe option should be passed as an option (in $options, the third argument to the constructor) rather than a new constructor argument.
It'd be really nice to use the header row to initialize csvcolumns, so the calling migration doesn't have to explicitly do it.
Thanks.
Comment #6
mikeryanI've committed my own version of the concept. Pass array('header_row'=>TRUE) as an option - the header will be skipped on import, and if you don't provide an explicit $csvcolumns list one will be generated from the header.
Comment #7
mikeryanI'm going to expand this support a bit... I'm dealing with CSV files with a bunch of lines up front listing "search parameters" etc. So, rather than making 'header_row' a boolean, let's make 'header_rows' an integer which is the number of rows to skip, with the last "skipped" row used to generate the CSV columns when $csvcolumns is empty.
Comment #8
mikeryanCommitted. If you were already using the 'header_row' argument, you need to change it to 'header_rows', and if you had been passing TRUE it would be preferable to pass 1 instead.