You are here

public function MigrateDestinationProfile2::import in Migrate Extras 7.2

Import a single entity.

Parameters

$entity: Entity object to build. Prefilled with any fields mapped in the Migration.

$row: Raw source data object - passed through to prepare/complete handlers.

Return value

array Array of key fields of the entity that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

File

./profile2.inc, line 109
Support for profile2 destinations.

Class

MigrateDestinationProfile2
Destination class implementing migration into nodes.

Code

public function import(stdClass $entity, stdClass $row) {
  $migration = Migration::currentMigration();
  $type = $this->entity_info['entity keys']['bundle'];
  $entity->{$type} = $this->bundle;
  list($id, $vid, $bundle) = entity_extract_ids($this->entity_type, $entity);

  // Updating previously-migrated content?
  if (isset($row->migrate_map_destid1)) {

    // Make sure is_new is off
    $entity->is_new = FALSE;
    if (!empty($id)) {
      if ($id != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming id !id and map destination id !destid1 don't match", array(
          '!id' => $id,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $entity->{$this->entity_key} = $row->migrate_map_destid1;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (empty($id)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided'));
    }
    $old_entity = entity_load_single($this->entity_type, $id);
    if (!isset($entity->created)) {
      $entity->created = $old_entity->created;
    }
    if (!isset($entity->uid)) {
      $entity->uid = $old_entity->uid;
    }
  }

  // Invoke migration prepare handlers
  $this
    ->prepare($entity, $row);

  // Trying to update an existing entity
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {

    // Incoming data overrides existing data.
    foreach ($entity as $field => $value) {
      $old_entity->{$field} = $value;
    }

    // Use the loaded entity from now on.
    $entity = $old_entity;
  }
  else {

    // Create a full profile class.
    $entity = entity_create($this->entity_type, (array) $entity);
  }
  if (empty($id) && !(isset($entity->is_new) && $entity->is_new)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  migrate_instrument_start('entity_save');
  entity_save($this->entity_type, $entity);
  migrate_instrument_stop('entity_save');
  list($id, $vid, $bundle) = entity_extract_ids($this->entity_type, $entity);
  if (isset($id)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    $return = array(
      $id,
    );
  }
  else {
    $return = FALSE;
  }
  $this
    ->complete($entity, $row);
  return $return;
}