Last updated May 13, 2016. Created on October 22, 2012.
Edited by merauluka, typhonius, jaspher, mikeryan. Log in to edit this page.

The migrate_d2d classes can be used as-is for migrating core data (e.g., user email, username, password, created date, etc.) with sensible defaults, but what about custom fields?

Suppose in your legacy system you had field_published on your article nodes, and in the new D7 system the equivalent field is field_published_date. You can map these fields by implementing a custom class overriding the provided node class and adding the mapping:

class ExampleArticleMigration extends DrupalNode6Migration {
  public function __construct(array $arguments) {
    parent::__construct($arguments);

    $this->addFieldMapping('field_published_date', 'field_published');
  }
}

To make use of your custom class, pass its name rather than DrupalNode6Migration when registering your classes:

Migration::registerMigration('ExampleArticleMigration',
  $article_node_arguments['machine_name'], $article_node_arguments)

Another common use case for overriding migration classes is modifying the base query. Each of the migrate_d2d classes has a query() method, returning the query object used to pull source data. By doing this rather than hard-coding the query into the constructor, you have the opportunity to customize the query for your application:

class ExampleUserMigration extends DrupalUser6Migration {
  protected function query() {
    // Get the default query (all rows in the users table other than uid 1)
    $query = parent::query();
    // Exclude test accounts
    $query->condition('name', 'test-%', 'NOT LIKE');
    // Add fields from a custom table
    $query->innerJoin('myextradata', 'd', 'u.uid=d.uid');
    $query->fields('d', array('favorite_food', 'favorite_song'));
    return $query;
  }
}

For more information on extending migration classes, see the Getting started with Migrate documentation page.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

pmackay’s picture

It would be super helpful if this page could be extended with an example of how to customise a d2d migration when going from nodes to entities for example. Is that possible with a customisation of migrate_d2d?

Summit’s picture

Would also great to have an example of nodetypes with specific fields which are not yet mapped out-of-the-box, like the weblinks module; https://www.drupal.org/project/weblinks

SomebodySysop’s picture

I created this .inc file (api declared in hook in module):

class CADEOGNodeMigration extends DrupalNode6Migration {

  public function __construct(array $arguments) {
    parent::__construct($arguments);
  }

  protected function query() {
    // Get the default query 
    $query = parent::query();

    // Get OG stuff as per: https://www.drupal.org/node/2033293#comment-9371471
     
    $query->leftJoin('og_ancestry','oga','n.nid = oga.nid');
    $query->addExpression('GROUP_CONCAT(DISTINCT og.group_nid)', 'og_group_ref');       

	$query->fields('oga');

    return $query;
  }
}

But I do not see the query updated with additional join when I look at Migrate d2d UI Node sources:

SELECT n.nid AS nid, n.vid AS vid, n.language AS language, n.title AS title, n.uid AS uid, n.status AS status, n.created AS created, n.changed AS changed, n.comment AS comment, n.promote AS promote, n.moderate AS moderate, n.sticky AS sticky, n.tnid AS tnid, n.translate AS translate, nr.body AS body, nr.teaser AS teaser, nr.format AS format, f.field_url_url AS field_url, f.field_url_title AS field_url_title, f.field_url_attributes AS field_url_attributes, nc.daycount AS daycount, nc.timestamp AS timestamp, nc.totalcount AS totalcount
FROM 
node n
INNER JOIN node_revisions nr ON n.vid=nr.vid
LEFT OUTER JOIN content_type_link f ON n.vid=f.vid
LEFT OUTER JOIN node_counter nc ON n.nid=nc.nid
WHERE  (n.type = 'link') 
ORDER BY n.changed ASC

If there is something else (besides including the above file in your .info file) required to extend DrupalNode6Migration in the way stated here, it really should be documented here.

SomebodySysop’s picture

This is my summary of how this "extending classes" works for MIgration newbies like me: https://www.drupal.org/node/2471863#comment-9840131

merauluka’s picture

Once you declare a new class you have add the class as a file in your .info file. Then you need to clear caches for Drupal to recognize your new file.

This is documented here: https://www.drupal.org/node/1006982