You are here

public function MigrateDestinationAsset::import in Asset 7

Import a single asset.

Parameters

stdClass $mapped_asset: Asset object to build. Prefilled with any fields mapped in the Migration.

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

Return value

array Array of key fields (aid only in this case) of the asset that was saved if successful. FALSE on failure.

Throws

MigrateException exception.

Overrides MigrateDestination::import

File

includes/asset.migrate.inc, line 99
Asset Migrate integration.

Class

MigrateDestinationAsset
Destination class implementing migration into assets.

Code

public function import(stdClass $mapped_asset, stdClass $row) {
  $asset = new Asset(array(
    'type' => $this->bundle,
  ));
  foreach ((array) $mapped_asset as $key => $value) {
    $asset->{$key} = $value;
  }

  // Updating previously-migrated content?
  $migration = Migration::currentMigration();
  if (isset($row->migrate_map_destid1)) {
    if (isset($asset->aid)) {
      if ($asset->aid != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming aid !aid and map destination aid !destid1 don't match", array(
          '!aid' => $asset->aid,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $asset->aid = $row->migrate_map_destid1;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($asset->aid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination aid provided'));
    }
    $old_asset = asset_load($asset->aid);
    if (!isset($asset->created)) {
      $asset->created = $old_asset->created;
    }
    if (!isset($asset->uid)) {
      $asset->uid = $old_asset->uid;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::SOURCE) {

    // Apply defaults, allow standard asset prepare hooks to fire.
    // asset_object_prepare() will blow these away, so save them here and
    // stuff them in later if need be.
    if (isset($asset->created)) {
      $asset->created = MigrationBase::timestamp($asset->created);
    }
    else {

      // To keep asset_object_prepare() from choking.
      $asset->created = REQUEST_TIME;
    }
    if (isset($asset->changed)) {
      $changed = MigrationBase::timestamp($asset->changed);
    }
    if (!isset($asset->uid)) {
      global $user;
      $asset->uid = $user->uid;
    }
  }

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

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

    // Incoming data overrides existing data, so only copy non-existent fields.
    foreach ($old_asset as $field => $value) {

      // An explicit NULL in the source data means to wipe to old value (i.e.,
      // don't copy it over from $old_node).
      if (property_exists($asset, $field) && $asset->{$field} === NULL) {

        // Ignore this field.
      }
      elseif (!isset($asset->{$field})) {
        $asset->{$field} = $old_asset->{$field};
      }
    }
  }
  if (isset($asset->aid)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  if (!empty($asset->title)) {
    migrate_instrument_start('asset_save');
    $asset
      ->save();
    migrate_instrument_stop('asset_save');
  }
  else {
    throw new MigrateException(t("Asset for the article !path wasn't created as long as it doesn't have any associated title.", array(
      '!path' => $row->lien,
    )));
  }
  if (isset($asset->aid)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    $return = array(
      $asset->aid,
    );
  }
  else {
    $return = FALSE;
  }
  $this
    ->complete($asset, $row);
  return $return;
}