I am importing some nodes from csv file in drupal.

I am assigning row number to one of the field of that content type.

The first row in the csv is the header which contains the column names only. The data start from the second row.

I am using the "csvrownum" for the csv row number. But it starts from the first row. It should exclude the first row as the data starts from the second row. How will I do this?

Here is my code:

$this->source = new MigrateSourceCSV($path, $columns, array('header_rows' => 1));
          //The destination CCK (boundle)
          $node_options = MigrateDestinationNode::options("en", "full_html");
          $this->destination = new MigrateDestinationNode('lot', $node_options);
          $this->map = new MigrateSQLMap(
              $this->machineName, array(
            "csvrownum" => array(
              'type' => 'int',
              'unsigned' => TRUE,
              'not null' => TRUE,
              'alias' => 'import'
              ), MigrateDestinationNode::getKeySchema()
          //Field mapping
          $this->addFieldMapping('title', 'Title');
          $this->addFieldMapping('field_number', 'csvrownum');
  • If any of the answers below worked for you, please flag them as such... – iain maitland Jun 18 '14 at 22:54
  • Could you elaborate on why you've unaccepted the answer? provide more information and so on. – pcambra Jul 1 '14 at 14:01

I think that's the expected behavior, you could use the prepareRow() method to increment csvrownum in whatever your header_rows is.

| improve this answer | |

Why not use the feeds module?

By default the parser is set to read Row 1. as a header row (although this can be turned off in the options) at the very least the code in 'FeedsCSVParser.inc' file will do something very similar to what you want and would be a good reference...cf. https://drupal.org/node/776854

| improve this answer | |

What worked for me recently is defining the $columns array and passing it to new MigrateSourceCSV.

You may also need to use single-quotes instead of double-quotes (inconsistent above).

| improve this answer | |

In order to skip first n lines you need to specify the 'header_rows' option.


$options = array(
  'header_rows' => 1,
  'delimiter' => ','

$csvcolumns = array(
  0 => array('title_nl', 'Title NL'),
  1 => array('title_en', 'Title EN'),
  2 => array('title_fr', 'Title FR'),

$this->source = new MigrateSourceCSV("/../filename.csv", $csvcolumns, $options);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.