Skip to content

Instantly share code, notes, and snippets.

@klaasvw
Created October 17, 2012 08:52
Show Gist options
  • Save klaasvw/3904524 to your computer and use it in GitHub Desktop.
Save klaasvw/3904524 to your computer and use it in GitHub Desktop.
Migrating translations using drupal migrate.
<?php
/**
* Migration class for migrating the source translations.
*/
class MySourceTranslationMigration extends Migration {
// Nothing special here. Just set up a node migration, migrating to nodes
// that will serve as translation sources.
// ...
public function __construct() {
// Set up everything for your migration.
// ...
// An example map.
$this->map = new MigrateSQLMap($this->machineName,
array(
'my_id' => array(
// ...
),
),
MigrateDestinationNode::getKeySchema()
);
}
}
/**
* Migration class for migrating the translations.
*/
class MyTranslationMigration extends Migration {
public function __construct() {
// Set up everything for your migration.
// ...
// Make sure the source translations are a dependency.
$this->dependencies = array('MySourceTranslation');
// Set up the mapping for the tnid. The second argument must be equal to
// the mapping key of the source translation migration.
// The sourceMigration method call ensures that we fetch the nid from the
// MySourceTranslation migration.
$this->addFieldMapping('tnid', 'my_id')->sourceMigration('MySourceTranslation');
// ...
}
/**
* Implements prepare().
*
* If we're translating a node we need to make sure the translation set is
* correctly initialized. This means the source translation needs its tnid set
* to its own nid. Drupal does this on insert when a node has a its
* translation source node assigned to a translation_source property.
*/
function prepare(&$node, $row) {
if (isset($node->tnid) && ($source = node_load($node->tnid))) {
$node->translation_source = $source;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment