You are here

public function MigrateDestinationFieldablePanelsPanes::import in Fieldable Panels Panes (FPP) 7

Import a single fieldable panels pane.

Parameters

object $fieldable_panels_pane: Fieldable panels pane 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 Array of key fields (fpid only in this case) of the fieldable panels pane that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

File

includes/fieldable_panels_pane.migrate.inc, line 116
Support for fieldable_panels_pane destinations.

Class

MigrateDestinationFieldablePanelsPanes
Destination class implementing migration into fieldable panels panes.

Code

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

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

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

    // Get the existing vid, tnid so updates don't generate notices.
    $values = db_select('fieldable_panels_panes', 'fpp')
      ->fields('fpp', array(
      'vid',
    ))
      ->condition('fpid', $fieldable_panels_pane->fpid)
      ->execute()
      ->fetchAssoc();
    if (empty($values)) {
      throw new MigrateException(t("Incoming fieldable panels pane ID !fpid no longer exists", array(
        '!fpid' => $fieldable_panels_pane->fpid,
      )));
    }
    $fieldable_panels_pane->vid = $values['vid'];
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($fieldable_panels_pane->fpid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination fpid provided'));
    }
    $old_fieldable_panels_panes = entity_load_single('fieldable_panels_pane', $fieldable_panels_pane->fpid);
    if (empty($old_fieldable_panels_panes)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but fieldable panels pane !fpid does not exist', array(
        '!fpid' => $fieldable_panels_pane->fpid,
      )));
    }
    if (!isset($fieldable_panels_pane->created)) {
      $fieldable_panels_pane->created = $old_fieldable_panels_panes->created;
    }
    if (!isset($fieldable_panels_pane->vid)) {
      $fieldable_panels_pane->vid = $old_fieldable_panels_panes->vid;
    }
  }
  if (!isset($fieldable_panels_pane->bundle)) {

    // Default the type to our designated destination bundle (by doing this
    // conditionally, we permit some flexibility in terms of implementing
    // migrations which can affect more than one type).
    $fieldable_panels_pane->bundle = $this->bundle;
  }

  // Set some required properties.
  if ($migration
    ->getSystemOfRecord() == Migration::SOURCE) {
    if (empty($fieldable_panels_pane->language)) {
      $fieldable_panels_pane->language = $this->language;
    }
    if (empty($fieldable_panels_pane->created)) {
      $fieldable_panels_pane->created = REQUEST_TIME;
    }
    if (empty($fieldable_panels_pane->updated)) {
      $fieldable_panels_pane->updated = $fieldable_panels_pane->created;
    }
  }

  // Invoke migration prepare handlers.
  $this
    ->prepare($fieldable_panels_pane, $row);
  if (!isset($fieldable_panels_pane->revision)) {

    // Saves disk space and writes. Can be overridden.
    $fieldable_panels_pane->revision = 0;
  }

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

    // Incoming data overrides existing data, so only copy non-existent
    // fields.
    if (!empty($old_fieldable_panels_panes)) {
      foreach ($old_fieldable_panels_panes 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_fieldable_panels_panes).
        if (property_exists($fieldable_panels_pane, $field) && $fieldable_panels_pane->{$field} === NULL) {

          // Ignore this field.
        }
        elseif (!isset($fieldable_panels_pane->{$field})) {
          $fieldable_panels_pane->{$field} = $old_fieldable_panels_panes->{$field};
        }
      }
    }
  }
  if (isset($fieldable_panels_pane->fpid) && !(isset($fieldable_panels_pane->is_new) && $fieldable_panels_pane->is_new)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  migrate_instrument_start('fieldable_panels_panes_save');
  entity_save('fieldable_panels_pane', $fieldable_panels_pane);
  migrate_instrument_stop('fieldable_panels_panes_save');
  if (isset($fieldable_panels_pane->fpid)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }

    // Unfortunately, http://drupal.org/node/722688 was not accepted, so fix
    // the changed timestamp.
    if (isset($changed)) {
      db_update('fieldable_panels_panes')
        ->fields(array(
        'changed' => $changed,
      ))
        ->condition('fpid', $fieldable_panels_pane->fpid)
        ->execute();
      $fieldable_panels_pane->changed = $changed;
    }

    // Potentially fix timestamp in fieldable_panels_panes_revision.
    $query = db_update('fieldable_panels_panes_revision')
      ->condition('vid', $fieldable_panels_pane->vid);
    if (isset($changed)) {
      $fields['timestamp'] = $changed;
    }
    if (!empty($fields)) {

      // We actually have something to update.
      $query
        ->fields($fields);
      $query
        ->execute();
      if (isset($changed)) {
        $fieldable_panels_pane->timestamp = $changed;
      }
    }
    $return = array(
      $fieldable_panels_pane->fpid,
    );
  }
  else {
    $return = FALSE;
  }
  $this
    ->complete($fieldable_panels_pane, $row);
  return $return;
}