Index: migrate_example/beer.migrate_example.inc
===================================================================
--- migrate_example/beer.migrate_example.inc (revision 135)
+++ migrate_example/beer.migrate_example.inc (working copy)
@@ -%ld,%ld +%ld,%ld @@
));
$this->destination = new MigrateDestinationEntity('node', 'migrate_example_beer');
- // Example of a single value image.
- $this->addFieldMapping(array('callback' => 'prepare_file', 'arguments' => array('source_field_name' => 'image', 'source_path' => drupal_get_path('module', 'migrate_example'))), 'field-migrate-example-image');
-
$this->addFieldMapping('name', 'title');
$this->addFieldMapping('aid', 'uid');
$this->addFieldMapping(NULL, 'sticky', 1);
// BEWARE. Replace underscores with dashes in field names. @see entity_metadata_field_property_set().
+ // Example of a single value image.
+ // Waiting on entity.module support: http://drupal.org/node/708268
+ // $this->addFieldMappingCallback('field-migrate-example-image', 'prepare_file', array('source_field_name' => 'image', 'source_path' => drupal_get_path('module', 'migrate_example'))));
+
// Example of delimited field handling. Also example of unformatted multi-value text field.
- $this->addFieldMapping(array('callback' => 'prepare_explode', 'arguments' => array('source_field_name' => 'countries', 'separator' => '|')), 'field-migrate-example-country');
+ $this->addFieldMappingCallback('field-migrate-example-country', 'prepare_explode', array('source_field_name' => 'countries', 'separator' => '|'));
// Example of a reference field. In this case, a term_reference.
// $this->addFieldMapping(array('callback' => 'prepare_keyed_object', 'arguments' => array('source_field_name' => 'countries', 'key' => 'tid')), 'taxonomy-tags');
// Example of a single value textfield with input format.
- $this->addFieldMapping(array('callback' => 'prepare_textfield', 'arguments' => array('source_field_name' => 'manufacturer', 'format' => 2)), 'body');
+ $this->addFieldMappingCallback('body', 'prepare_textfield', array('source_field_name' => 'manufacturer', 'format' => 2));
}
}
@@ -%ld,%ld +%ld,%ld @@
$this->addFieldMapping('name', 'name');
$this->addFieldMapping('manufacturer_mail', 'mail');
// Add the rid=3 for all users. TODO: not working. Asked fago at http://drupal.org/node/705370.
- $this->addFieldMapping(array('callback' => 'prepare_fixed', 'arguments' => array(2, 3)), 'roles');
+ $this->addFieldMappingCallback('roles', 'prepare_fixed', array(2, 3));
}
}
@@ -%ld,%ld +%ld,%ld @@
$this->addFieldMapping('description', 'description');
// This tells us the name of the map table. Perhaps there is a better way.
$this->ensureTables();
- $this->addFieldMapping(array('callback' => 'prepare_parent_term', 'arguments' => array('map_table_name' => $this->map_table_name)), 'parent');
+ $this->addFieldMappingCallback('parent', 'prepare_parent_term', array('map_table_name' => $this->map_table_name));
}
}
class MigrateDestinationEntityBeer extends MigrateDestinationEntity {
- // A fieldMapping callback for term creation. Get tid of the parent_name.
+ // A fieldMappingCallback for term creation. Get tid of the parent_name.
function prepare_parent_term($row, $arguments) {
if (!empty($row->parent_name)) {
return db_query("SELECT destid FROM :map_table WHERE sourceid = :sourceid", array(':map_table' => $arguments['map_table_name'], ':sourceid' => $row->parent_name))->fetchField();
Index: includes/migration.inc
===================================================================
--- includes/migration.inc (revision 135)
+++ includes/migration.inc (working copy)
@@ -%ld,%ld +%ld,%ld @@
public $name, $description, $source, $destination;
public $map_table_name, $message_table_name;
- protected $fieldMappings = array(), $defaultValues = array();
+ protected $fieldMappings = array(), $fieldMappingsCallback = array(), $defaultValues = array();
protected $currSourceKeys = array();
protected $sourceKeyMap, $destinationKeyMap;
@@ -%ld,%ld +%ld,%ld @@
////////////////////////////////////////////////////////////////////
// Processing
- // Specify field mapping and their prepare callbacks if needed.
+ // Specify simple field mapping.
public function addFieldMapping($sourceField, $destinationField, $defaultValue = NULL) {
$this->fieldMappings[$destinationField] = $sourceField;
$this->defaultValues[$destinationField] = $defaultValue;
}
+ // Specify field mapping and its prepare callback.
+ public function addFieldMappingCallback($destinationField, $callback, $arguments, $defaultValue = NULL) {
+ $this->fieldMappingsCallback[$destinationField] = array($callback, $arguments);
+ $this->defaultValues[$destinationField] = $defaultValue;
+ }
+
public function rollback() {
$this->ensureTables();
// TODO: Support multi-field keys
@@ -%ld,%ld +%ld,%ld @@
$this->ensureTables();
while ($data_row = $this->source->fetch($this)) {
// TODO: Check memory
+ // Simple mapping.
foreach ($this->fieldMappings as $destination => $source) {
- if (is_array($source)) {
- // Invoke a FieldMapping callback.
- $values[$destination] = $this->destination->$source['callback']($data_row, @$source['arguments'], $destination);
- }
- elseif (isset($data_row->$source) && $data_row->$source) {
- // Simple mapping.
+ if (isset($data_row->$source) && $data_row->$source) {
$values[$destination] = $data_row->$source;
}
elseif (isset($this->defaultValues[$destination])) {
@@ -%ld,%ld +%ld,%ld @@
}
}
+ // These mapping run through a prepare function.
+ foreach ($this->fieldMappingsCallback as $destination => $pair) {
+ // $pair is an array like: array($callback, $arguments);
+ $values[$destination] = $this->destination->$pair[0]($data_row, $pair[1], $destination);
+ }
+
// Track the current source key
// If there's a destination ID, the intent is to update an existing object,