How to use prepareKey() for the Migrate module 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-&gt;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.
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-&gt;glid = crc32($row-&gt;xml-&gt;name);
  $key['glid'] = $row-&gt;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-&gt;map = new MigrateSQLMap($this-&gt;machineName, array(
  'glid' =&gt; array(
    'type' =&gt; 'varchar',
    'length' =&gt; '255',
    'not null' =&gt; TRUE,
    'description' =&gt; 'id generated from a string'
  ),
), MigrateDestinationNode::getKeySchema()
);


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