You are here

public function MigrateDestinationParagraphsItem::import in Paragraphs 7

Import a single node.

Parameters

object $paragraphs_item: Node object to build Prefilled with any fields mapped in the Migration.

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

Return value

array|bool Array of key fields (nid only in this case) of the node that was saved if successful. FALSE on failure.

Throws

MigrateException

Overrides MigrateDestination::import

File

migrate/destinations/MigrateDestinationParagraphsItem.inc, line 115

Class

MigrateDestinationParagraphsItem
Provides basic class for importing Paragraphs via Migrate API.

Code

public function import(object $paragraphs_item, object $row) {
  $migration = Migration::currentMigration();

  // Updating previously-migrated content?
  if (isset($row->migrate_map_destid1)) {
    if (isset($paragraphs_item->item_id)) {
      if ($paragraphs_item->item_id != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming item_id !item_id and map destination item_id !destid1 don't match", array(
          '!item_id' => $paragraphs_item->item_id,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $paragraphs_item->item_id = $row->migrate_map_destid1;
      $paragraphs_item->is_new = FALSE;
      $paragraphs_item->is_new_revision = FALSE;

      // Get the existing revision_id so updates don't generate notices.
      $values = db_select('paragraphs_item', 'p')
        ->fields('p', array(
        'revision_id',
      ))
        ->condition('item_id', $paragraphs_item->item_id)
        ->execute()
        ->fetchAssoc();
      if (empty($values)) {
        throw new MigrateException(t("Incoming paragraphs ID !item_id no longer exists", array(
          '!item_id' => $paragraphs_item->item_id,
        )));
      }
      $paragraphs_item->revision_id = $values['revision_id'];
    }
  }

  // When updating, we make sure that id exists.
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($paragraphs_item->item_id)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination item_id provided'));
    }

    // Hold raw original values for later.
    $raw_paragraph = $paragraphs_item;

    // This entity will be the one, we party on.
    $entity = paragraphs_item_load($paragraphs_item->item_id);
    if (empty($entity)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but paragraphs item !item_id does not exist', array(
        '!item_id' => $paragraphs_item->item_id,
      )));
    }
  }
  else {

    // Set some default properties.
    $defaults = array(
      'language' => $this->language,
      'bundle' => $this->bundle,
      'field_name' => $this->field_name,
      'archived' => 0,
    );
    foreach ($defaults as $field => $value) {
      if (!isset($paragraphs_item->{$field})) {
        $paragraphs_item->{$field} = $value;
      }
    }
  }
  $this
    ->prepare($paragraphs_item, $row);
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    foreach ($raw_paragraph as $field => $value) {
      $entity->{$field} = $paragraphs_item->{$field};
    }
  }
  else {

    // This will be the entity we party on.
    $entity = entity_create('paragraphs_item', (array) $paragraphs_item);
  }
  if (isset($entity->item_id) && $entity->item_id) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  migrate_instrument_start('paragraphs_item_save');
  $entity
    ->save(TRUE);
  migrate_instrument_stop('paragraphs_item_save');
  $this
    ->complete($entity, $row);
  if (isset($entity->item_id) && $entity->item_id > 0) {
    $return = array(
      $entity->item_id,
    );
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
  }
  else {
    $return = FALSE;
  }
  return $return;
}