What is the file name I have to add the following script in it?

This is actually easy to do. In the prepare() function of the node migration class, just set the nid and is_new to true:

public function prepare($node, $row) {
  // Preserve old nid so that old links to nodes using /node/nid still work
  $node->is_new = TRUE;
  $node->nid = $row->nid;

Source link: How can I preserve nids during a migration?


Unfortunately, sometimes answers on stackexchange are misleading. It doesn't look like ANY classes within migrate_d2d extend MigrateDestinationEntity, that invokes a "prepare" hook. So I think the question itself is misguided.

Looking in the migrate code, it looks like any class that extends abstract class MigrateDestinationEntity can call this process function see /migrate/plugins/destinations/entity.inc,

on line 140:

// Then call any entity-specific handlers
migrate_handler_invoke_all($this->entityType, 'prepare', $entity, $source_row);

Rather than trying to find a place to call this from DrupalNodeMigration, which will never call this function, what you need to do is add a mapping for nid, and a mapping for "is_new".

In your class that extends the DrupalNodeMigration class, in the __contruct() function, just make sure to add a mapping for nid and is_new.

$this->addFieldMapping('nid', 'nid');

Otherwise, nid will be automatically assigned.


I understand why you're asking, that answer is not really clear, or very generic at best. Let's see if we can figure out what they meant.

The prepare() methods of Migration classes are invoked from the prepare() method found on the MigrateDestinationEntity class (migrate/plugins/destinations/entity.inc). In the case of a d2d node migration, the Migration class is DrupalNodeMigration (migrate_d2d/node.inc). Other d2d Migration classes, like DrupalRoleMigration, also implement a prepare() method. So, my conclusion would be that this code should be added in DrupalNodeMigration.

Disclaimer: I didn't test this, it is just my conclusion based on reading the answer and code.

I must add that editing the code of a contributed module is never recommended. A cleaner, but more time consuming way would be to create a new Migration class in a custom module, make it extend DrupalNodeMigration, and add the prepare method there. How to do that exactly is beyond the scope of this question.

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.