protected function Migration::handleSourceMigration in Migrate 7.2
Same name and namespace in other branches
- 6.2 includes/migration.inc \Migration::handleSourceMigration()
Look up a value migrated in another migration.
Parameters
mixed $source_migrations: An array of source migrations, or string for a single migration.
mixed $source_keys: Key(s) to be looked up against the source migration(s). This may be a simple value (one single-field key), an array of values (multiple single-field keys to each be looked up), or an array of arrays (multiple multi-field keys to each be looked up).
mixed $default: The default value, if no ID was found.
$migration: The implementing migration.
Return value
Destination value(s) from the source migration(s), as a single value if a single key was passed in, or an array of values if there were multiple keys to look up.
1 call to Migration::handleSourceMigration()
- Migration::applyMappings in includes/
migration.inc - Apply field mappings to a data row received from the source, returning a populated destination object.
File
- includes/
migration.inc, line 1454 - Defines the base class for import/rollback processes.
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,…
Code
protected function handleSourceMigration($source_migrations, $source_keys, $default = NULL, $migration = NULL) {
// Handle the source migration(s) as an array.
$source_migrations = (array) $source_migrations;
// We want to treat source keys consistently as an array of arrays (each
// representing one key).
if (is_array($source_keys)) {
if (empty($source_keys)) {
// Empty value should return empty results.
return NULL;
}
elseif (is_array(reset($source_keys))) {
// Already an array of key arrays, fall through
}
else {
// An array of single-key values - make each one an array
$new_source_keys = array();
foreach ($source_keys as $source_key) {
$new_source_keys[] = array(
$source_key,
);
}
$source_keys = $new_source_keys;
}
}
else {
// A simple value - make it an array within an array
$source_keys = array(
array(
$source_keys,
),
);
}
// Instantiate each migration, and store back in the array.
foreach ($source_migrations as $key => $source_migration) {
$source_migrations[$key] = Migration::getInstance($source_migration);
if (!isset($source_migrations[$key])) {
MigrationBase::displayMessage(t('The @source cannot be resolved to a migration instance.', array(
'@source' => $source_migration,
)));
unset($source_migrations[$key]);
}
}
$results = array();
// Each $source_key will be an array of key values
foreach ($source_keys as $source_key) {
// If any source keys are NULL, skip this set
$continue = FALSE;
foreach ($source_key as $value) {
if (!isset($value)) {
$continue = TRUE;
break;
}
}
// Occasionally $source_key comes through with an empty string.
$sanity_check = array_filter($source_key, 'strlen');
if ($continue || empty($source_key) || empty($sanity_check)) {
continue;
}
// Loop through each source migration, checking for an existing dest ID.
foreach ($source_migrations as $source_migration) {
// Break out of the loop as soon as a destination ID is found.
if ($destids = $source_migration
->getMap()
->lookupDestinationID($source_key)) {
if (!empty($destids['destid1'])) {
break;
}
}
}
// If no destination ID was found, give each source migration a chance to
// create a stub.
if (!$destids) {
foreach ($source_migrations as $source_migration) {
// Is this a self reference?
if ($source_migration->machineName == $this->machineName) {
if (!array_diff($source_key, $this
->currentSourceKey())) {
$destids = array();
$this->needsUpdate = MigrateMap::STATUS_NEEDS_UPDATE;
break;
}
}
// Break out of the loop if a stub was successfully created.
if ($destids = $source_migration
->createStubWrapper($source_key, $migration)) {
break;
}
}
}
if ($destids) {
// Assume that if the destination key is a single value, it
// should be passed as such
if (count($destids) == 1) {
$results[] = reset($destids);
}
else {
$results[] = $destids;
}
}
elseif (!is_null($default)) {
$results[] = $default;
}
}
// Return a single result if we had a single key
if (count($source_keys) > 1) {
return $results;
}
else {
$value = reset($results);
return empty($value) && $value !== 0 && $value !== '0' ? NULL : $value;
}
}