I am trying to do write a migration class extending the Migrate module. The migration will be from an XML file so I will want to extend the XMLMigration class. Based on the Migrate modules wine.inc example, I'm going to have to something like this:

class WineProducerXMLPullMigration extends XMLMigration {
 public function __construct($arguments) {
  $item_xpath = '/producers/producer';  // relative to document
  $item_ID_xpath = 'sourceid';          // relative to item_xpath

  $this->source = new MigrateSourceXML($items_url, $item_xpath,
                                       $item_ID_xpath, $fields);

My issue is that my xml doesn't have any unique ID per row, and therefore I can't really pass a $item_ID_path. I read this post https://www.drupal.org/node/1444356#comment-6974360 and found that the prepareKey() function is supposed to be used for this purpose, however as far as I can tell, you don't ever call this directly, and its called after this class is finished processing. This being the case, I'm not sure what I should be passing in $item_ID_xpath and it is a required field.

1 Answer 1


You can set the $item_ID_xpath to something that does exist in the XML, for example 'name'

Then inside your migration class, you can use the prepareKey method to generate an ID

public function prepareKey($source_key, $row) {
  $row->glid = crc32($row->xml->name);
  $key['glid'] = $row->glid;
  return $key;

I am using crc32() to generate an ID from a string, but I have no clue whether that is smart or not, so use at own risk.

Now that you have an ID ('glid' in the example) you use in the mapping:

$this->map = new MigrateSQLMap($this->machineName, array(
  'glid' => array(
    'type' => 'varchar',
    'length' => '255',
    'not null' => TRUE,
    'description' => 'id generated from a string'
), MigrateDestinationNode::getKeySchema()

Dont forget to deregister the migration if you change this schema.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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