You are here

public function MigrateDestinationRelation::import in Relation 8

Same name and namespace in other branches
  1. 8.2 relation_migrate/relation_migrate.destination.inc \MigrateDestinationRelation::import()
  2. 7 relation_migrate/relation_migrate.destination.inc \MigrateDestinationRelation::import()

Import a single relation.

Parameters

$relation: Relation 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 (relation ID only in this case) of the relation that was saved if successful. FALSE on failure.

File

relation_migrate/relation_migrate.destination.inc, line 88
Support for relation destinations.

Class

MigrateDestinationRelation
Destination class implementing migration into relation.

Code

public function import(stdClass $relation, stdClass $row) {

  // Updating previously-migrated content?
  $migration = Migration::currentMigration();
  if (isset($row->migrate_map_destid1)) {

    // Make sure is_new is off.
    $relation->is_new = FALSE;
    $relation_id = $relation
      ->id();
    if (isset($relation_id)) {
      if ($relation_id != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming relation_id !relation_id and map destination relation_id !destid1 don't match", array(
          '!relation_id' => $relation
            ->id(),
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $relation
        ->set('id', $row->migrate_map_destid1);
    }

    // Get the existing revision_id, so updates don't generate notices.
    $values = \Drupal::database()
      ->select('relation', 'r')
      ->fields('r', array(
      'revision_id',
    ))
      ->condition('relation_id', $relation
      ->id())
      ->execute()
      ->fetchAssoc();
    $relation
      ->set('revision_id', $values['revision_id']);
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    $relation_id = $relation
      ->id();
    if (!isset($relation_id)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination relation_id provided'));
    }
    $old_relation = Relation::load($relation
      ->id());
    if (!isset($relation->created)) {
      $relation->created = $old_relation->created;
    }
    $relation_revision_id = $relation
      ->getRevisionId();
    if (!isset($relation_revision_id)) {
      $relation
        ->set('revision_id', $old_relation
        ->getRevisionId());
    }
    if (!isset($relation->uid)) {
      $relation->uid = $old_relation->uid;
    }
  }

  // Set some required properties.
  if (!isset($relation->uid)) {
    $relation->uid = $GLOBALS['user']->uid;
  }

  // Set type before invoking prepare handlers - they may take type-dependent actions.
  $relation->relation_type = $this->bundle;
  if ($migration
    ->getSystemOfRecord() == Migration::SOURCE) {

    // relation_save() will blow these away, so save them here and
    // save them later.
    if (isset($relation->created)) {
      $created = MigrationBase::timestamp($relation->created);
    }
    if (isset($relation->changed)) {
      $changed = MigrationBase::timestamp($relation->changed);
    }
  }

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

    // Incoming data overrides existing data, so only copy non-existent fields.
    foreach ($old_node 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($relation, $field) && $relation->{$field} === NULL) {

        // Ignore this field.
      }
      elseif (!isset($relation->{$field})) {
        $relation->{$field} = $old_relation->{$field};
      }
    }
  }

  // Invoke migration prepare handlers.
  $this
    ->prepare($relation, $row);
  $relation_id = $relation
    ->id();
  if (isset($relation_id) && empty($relation->is_new)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }

  // Save relation object.
  migrate_instrument_start('relation_save');
  $relation_id = relation_save($relation);
  migrate_instrument_stop('relation_save');
  if (isset($relation_id)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }

    // Update changed and created dates if needed.
    if (isset($changed)) {
      \Drupal::database()
        ->update('relation')
        ->fields(array(
        'changed' => $changed,
      ))
        ->condition('relation_id', $relation
        ->id())
        ->execute();
      $relation->changed = $changed;
    }
    if (isset($created)) {
      \Drupal::database()
        ->update('relation')
        ->fields(array(
        'created' => $created,
      ))
        ->condition('relation_id', $relation
        ->id())
        ->execute();
      $relation->created = $created;
    }
    $return = array(
      $relation
        ->id(),
    );
  }
  else {
    $return = FALSE;
  }
  $this
    ->complete($relation, $row);
  return $return;
}