public function MigrateDestinationEntityAPI::import in Migrate Extras 7.2
Imports a single entity.
Parameters
stdClass $entity: Generic entity object, refilled with any fields mapped in the Migration.
stdClass $row: Raw source data object - passed through to prepare/complete handlers.
Return value
array An array of key fields (entity id, and revision id if applicable) of the entity that was saved if successful. FALSE on failure.
Overrides MigrateDestination::import
File
- ./
entity_api.inc, line 168 - Support for entity types implementing the Entity API.
Class
- MigrateDestinationEntityAPI
- Destination class implementing migration into entity types.
Code
public function import(stdClass $entity, stdClass $row) {
$migration = Migration::currentMigration();
// Updating previously-migrated content?
if (isset($row->migrate_map_destid1)) {
if (isset($entity->{$this->id})) {
if ($entity->{$this->id} != $row->migrate_map_destid1) {
throw new MigrateException(t("Incoming id !id and map destination id !destid1 don't match", array(
'!id' => $entity->{$this->id},
'!destid1' => $row->migrate_map_destid1,
)));
}
}
else {
$entity->{$this->id} = $row->migrate_map_destid1;
}
}
elseif ($migration
->getSystemOfRecord() == Migration::SOURCE) {
unset($entity->{$this->id});
}
if (isset($row->migrate_map_destid2)) {
if (isset($entity->{$this->revision})) {
if ($entity->{$this->revision} != $row->migrate_map_destid2) {
throw new MigrateException(t("Incoming revision !id and map destination revision !destid2 don't match", array(
'!id' => $entity->{$this->revision},
'!destid2' => $row->migrate_map_destid2,
)));
}
}
else {
$entity->{$this->revision} = $row->migrate_map_destid2;
}
}
if ($migration
->getSystemOfRecord() == Migration::DESTINATION) {
if (!isset($entity->{$this->id})) {
throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided'));
}
// Load the entity that's being updated, update its values, then
// substitute the (fake) passed in entity with that one.
$old_entity = entity_load_single($this->entityType, $entity->{$this->id});
if (empty($old_entity)) {
throw new MigrateException(t("Failed to load entity of type %type and id %id", array(
'%type' => $this->entityType,
'%id' => $entity->{$this->id},
)));
}
// Prepare the entity to get the right array structure.
$this
->prepare($entity, $row);
foreach ($entity as $field => $value) {
$old_entity->{$field} = $entity->{$field};
}
$entity = $old_entity;
}
else {
// Create a real entity object, update its values with the ones we have
// and pass it along.
$new_entity = array();
if (!empty($this->bundle) && !empty($this->info['entity keys']['bundle'])) {
$new_entity[$this->info['entity keys']['bundle']] = $this->bundle;
}
$new_entity = entity_create($this->entityType, $new_entity);
foreach ($entity as $field => $value) {
$new_entity->{$field} = $entity->{$field};
}
// If a destination id exists, the entity is obviously not new.
if (!empty($new_entity->{$this->id}) && isset($new_entity->is_new)) {
unset($new_entity->is_new);
}
$entity = $new_entity;
$this
->prepare($entity, $row);
}
$updating = !empty($entity->{$this->id}) && empty($entity->is_new);
migrate_instrument_start('entity_save');
entity_save($this->entityType, $entity);
// It's probably not worth keeping the static cache around.
entity_get_controller($this->entityType)
->resetCache();
migrate_instrument_stop('entity_save');
$this
->complete($entity, $row);
if (isset($entity->{$this->id}) && $entity->{$this->id} > 0) {
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
$return = array(
$entity->{$this->id},
);
if (isset($entity->{$this->revision}) && $entity->{$this->revision} > 0) {
$return[] = array(
$entity->{$this->revision},
);
}
return $return;
}
return FALSE;
}