Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Get your bits in!

How to use the Migrate module to populate your Drupal site

Elliot Ward

on 24 November 2012

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Get your bits in!

Get Your Bits In! ...or how to use the Migrate module to populate your Drupal site Elliot Ward Senior developer at Capgemini eli-t eli_t 1 0 http://drupal.org/project/migrate The migrate module provides a flexible framework for migrating content into Drupal from other sources (e.g., when converting a web site from another CMS to Drupal). Out-of-the-box, support for creating core Drupal objects such as nodes, users, files, terms, and comments are included - it can easily be extended for migrating other kinds of content. Content is imported and rolled back using a bundled web interface (Migrate UI module) or included Drush commands (strongly recommended). migrate MySQL MSSQL CSV JSON Oracle WordPress PhpBB Drupal IRC XML your
here? nodes comments terms users your
here? migrate autoload dbtng { d6 migrate_extras migrate_ui my_migrate abstract class Migration /**
* The base class for all import objects. This is where most of the smarts
* of the migrate module resides. Migrations are created by deriving from this
* class, and in the constructor (after calling parent::__construct()) initializing
* at a minimum the name, description, source, and destination properties. The constructor
* will also usually make several calls to addFieldMapping().
*/ Subclasses of Migration must have: MigrateGroup $source $destination $map passed to parent constructor subclass of MigrateSource subclass of MigrateDestination instance of MigrateSQLMap menu items MigrateGroup $source eli-t class MigrationNodeMonkey extends Migration {
public function __construct() {
$this->description = t('Migrate the Monkeys'); class MigrationNodeSloth extends Migration {
public function __construct() {
$this->description = t('Migrate the Sloths'); class MigrationNodeTree extends Migration {
public function __construct() {
$this->description = t('Migrate the Trees'); my_migrate.info name = "My Migrate"
description = "Contrived migrate module for example purposes during talk"
package = "My Migration Packages"
core = 7.x
dependencies[] = migrate
dependencies[] = migrate_extras
files[] = nodes/migration_node_monkey.inc
files[] = nodes/migration_node_sloth.inc
files[] = nodes/migration_node_tree.inc
files[] = terms/migration_term_habitat.inc
files[] = nodes/migration_term_phylogeny.inc my_migrate.module <?php

* Implements hook_migrate_api().
* Specifies we are using v2 of the migrate api
* @return int

function my_migrate_migrate_api() {
$api = array(
'api' => 2,
return $api;

// this changed as of version 2.5 and now migration classes should be explicitly registered here

nodes/migration_node_trees.inc nodes/migration_node_monkey.inc nodes/migration_node_sloth.inc nodes/migration_node_tree.inc // create query and add columns from node table
$node_columns = array(

$query = Database::getConnection('default', 'source_db')
->select('node', 'n')
->fields('n', $node_columns)
->condition('type', 'monkey');
$query->addField('n', 'nid');


// Create a MigrateSource object, which manages retrieving the input data.
// map_joinable is false as the map table can't be joined to the source as they are in different DBs
$this->source = new MigrateSourceSQL($query, array(), NULL, array('map_joinable' => FALSE)); $destination // Set up our destination
$this->destination = new MigrateDestinationNode('monkey'); $map $map // Set up mapping between source and destination
$this->map = new MigrateSQLMap($this->machineName,
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'ID of source node',
'alias' => 'n',
); source destination map sourceid1 destid1 Checking Status - migrate_ui // create query and add columns from node table
$node_columns = array(

// alternative form for each column
// $this->addFieldMapping('format', 'format'); Checking Status - migrate_ui Checking Status - migrate_ui Checking Status - drush Checking Status - drush drush migrate-status / ms drush migrate-audit / ma defaults to all classes
supports --group or class name eg
drush ms MigrationNodeMonkey
drush ms --group=AnimalNodeMigration Simple Field Mappings Checking Status Checking Status // Add explicitly unmigrated destination fields
$unmigrated_destinations = array(
$this->addUnmigratedDestinations($unmigrated_destinations); LIVE DEMO
APPROACHING Adding Fields // join body field
$query->leftJoin('field_data_body', 'fdb', 'n.vid = fdb.entity_id');
$query->addField('fdb', 'body_value');
$query->addField('fdb', 'body_summary');
$query->addField('fdb', 'body_format');
$query->addField('fdb', 'language', 'body_language');

$this->addFieldMapping('body', 'body_value');
$this->addFieldMapping('body:summary', 'body_summary');
$this->addFieldMapping('body:format', 'body_format');
$this->addFieldMapping('body:language', 'body_language'); LIVE DEMO
APPROACHING Dealing with simple dependencies monkeys trees favourite monkeys trees favourite weapons Ensure the trees mapped 1st Remap entity references when migrating monkeys $this->dependencies[] = 'MigrationNodeTree'; $query->leftJoin('field_data_field_favourite_tree', 'fdfft', 'n.vid = fdfft.entity_id');
$query->addField('fdfft', 'field_favourite_tree_target_id');

$this->addFieldMapping('field_favourite_tree', 'field_favourite_tree_target_id')
->sourceMigration('MigrationNodeTree'); Dealing with less simple dependencies monkeys best friend monkeys protected function createStub($migration, $source_id) {
$node = new stdClass();
$node->title = t('Stub for') . $source_id[0];
$node->body = t('Stub body');
$node->type = $this->destination->getBundle();
$node->uid = 1;
$node->status = 0;
if (isset($node->nid)) {
return array($node->nid);
else {
return FALSE;
} // Monkeys' best friend
$query->leftJoin('field_data_field_best_friend', 'fdfbf', 'n.vid = fdfbf.entity_id');
$query->addField('fdfbf', 'field_best_friend_target_id');

$this->addFieldMapping('field_best_friend', 'field_best_friend_target_id')
->sourceMigration('MigrationNodeMonkey'); Eli_TW elements // join image field
$query->leftJoin('field_data_field_picture', 'fdfp', 'n.vid = fdfp.entity_id');
$query->addField('fdfp', 'field_picture_title');

// join file_managed
$query->leftJoin('file_managed', 'fm', 'fdfp.field_picture_fid = fm.fid');
$query->addField('fm', 'filename');

$this->addFieldMapping('field_picture', 'filename');
$this->addFieldMapping('field_picture:title', 'field_picture_title');
->defaultValue('/home/quickstart/websites/source.local/sites/default/files'); LIVE DEMO
Full transcript