You are here

public function MigrateDestinationTerm::import in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/term.inc \MigrateDestinationTerm::import()

Import a single term.

Parameters

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

Overrides MigrateDestination::import

File

plugins/destinations/term.inc, line 142
Support for taxonomy term destinations.

Class

MigrateDestinationTerm
Destination class implementing migration into terms.

Code

public function import(stdClass $term, stdClass $row) {
  $migration = Migration::currentMigration();

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

  // Default to bundle if no vocabulary machine name provided
  if (!isset($term->vocabulary_machine_name)) {
    $term->vocabulary_machine_name = $this->bundle;
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($term->tid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination tid provided'));
    }
    $rawterm = $term;
    $this
      ->prepare($term, $row);
    $old_term = taxonomy_term_load($term->tid);
    if (empty($old_term)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but term !tid does not exist', array(
        '!tid' => $term->tid,
      )));
    }
    foreach ($rawterm as $field => $value) {
      $old_term->{$field} = $term->{$field};
    }
    $term = $old_term;
  }
  else {

    // vid is required
    if (empty($term->vid)) {
      static $vocab_map = array();
      if (!isset($vocab_map[$term->vocabulary_machine_name])) {

        // The keys of the returned array are vids
        $vocabs = taxonomy_vocabulary_load_multiple(array(), array(
          'machine_name' => $term->vocabulary_machine_name,
        ));
        $vids = array_keys($vocabs);
        if (isset($vids[0])) {
          $vocab_map[$term->vocabulary_machine_name] = $vids[0];
        }
        else {
          $migration
            ->saveMessage(t('No vocabulary found with machine_name !name', array(
            '!name' => $term->vocabulary_machine_name,
          )));
          return FALSE;
        }
      }
      $term->vid = $vocab_map[$term->vocabulary_machine_name];
    }

    // Look up parent name if provided
    if (isset($term->parent_name) && trim($term->parent_name)) {

      // Look for the name in the same vocabulary.
      // Note that hierarchies may have multiples of the same name...
      $terms = taxonomy_term_load_multiple(array(), array(
        'name' => trim($term->parent_name),
        'vid' => $term->vid,
      ));
      $tids = array_keys($terms);
      $term->parent = array(
        $tids[0],
      );
      unset($term->parent_name);
    }
    if (empty($term->parent)) {
      $term->parent = array(
        0,
      );
    }
    elseif (!is_array($term->parent)) {

      // Convert to an array for comparison in findMatchingTerm().
      // Note: taxonomy_term_save() also normalizes to an array.
      $term->parent = array(
        $term->parent,
      );
    }
    if (isset($term->parent['arguments'])) {

      // Unset arguments here to avoid duplicate entries in the
      // term_hierarchy table.
      unset($term->parent['arguments']);
    }
    if (!isset($term->format)) {
      $term->format = $this->textFormat;
    }
    if (!isset($term->language)) {
      $term->language = $this->language;
    }
    $this
      ->prepare($term, $row);
    if (empty($term->name)) {
      throw new MigrateException(t('Taxonomy term name is required.'));
    }
    if (!$this->allowDuplicateTerms && ($existing_term = $this
      ->findMatchingTerm($term))) {
      foreach ($existing_term as $field => $value) {
        if (!isset($term->{$field})) {
          $term->{$field} = $existing_term->{$field};
        }
      }
    }
  }

  // Trying to update an existing term
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    $existing_term = taxonomy_term_load($term->tid);
    if ($existing_term) {

      // Incoming data overrides existing data, so only copy non-existent fields
      foreach ($existing_term as $field => $value) {
        if (!isset($term->{$field})) {
          $term->{$field} = $existing_term->{$field};
        }
      }
    }
  }
  if (isset($term->tid)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }

  // Validate field data prior to saving.
  MigrateDestinationEntity::fieldAttachValidate('taxonomy_term', $term);
  migrate_instrument_start('taxonomy_term_save');
  $status = taxonomy_term_save($term);
  migrate_instrument_stop('taxonomy_term_save');
  $this
    ->complete($term, $row);
  if (isset($term->tid)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    $return = array(
      $term->tid,
    );
  }
  else {
    $return = FALSE;
  }
  return $return;
}