You are here

public function MigrateTaxonomyTermReferenceFieldHandler::prepare in Migrate 7.2

File

plugins/destinations/fields.inc, line 502
Support for processing entity fields

Class

MigrateTaxonomyTermReferenceFieldHandler

Code

public function prepare($entity, array $field_info, array $instance, array $values) {
  if (isset($values['arguments'])) {
    $arguments = $values['arguments'];
    unset($values['arguments']);
  }
  else {
    $arguments = array();
  }
  if (count($values) == 1 && empty($values[0])) {
    $values = array();
  }
  $tids = array();
  if (isset($arguments['source_type']) && $arguments['source_type'] == 'tid') {

    // Nothing to do. We have tids already.
    $tids = $values;
  }
  elseif ($values) {
    $vocab_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
    $names = taxonomy_vocabulary_get_names();

    // Get the vocabulary for this term.
    if (isset($field_info['settings']['allowed_values'][0]['vid'])) {
      $vid = $field_info['settings']['allowed_values'][0]['vid'];
    }
    else {
      $vid = $names[$vocab_name]->vid;
    }

    // Remove leading and trailing spaces in term names.
    $values = array_map('trim', $values);

    // Remove empty array values from $values array.
    if (!empty($arguments['not_empty'])) {
      $terms = array();
      foreach ($values as $term) {
        if (!empty($term)) {
          $terms[] = $term;
        }
        $values = $terms;
      }
    }

    // Cannot use taxonomy_term_load_multiple()
    // since we have an array of names.
    // It wants a singular value. This query may return case-insensitive
    // matches.
    $existing_terms = db_select('taxonomy_term_data', 'td')
      ->fields('td', array(
      'tid',
      'name',
    ))
      ->condition('td.name', $values, 'IN')
      ->condition('td.vid', $vid)
      ->execute()
      ->fetchAllKeyed(1, 0);

    // If we're ignoring case, change both the matched term name keys and the
    // source values to lowercase.
    if (isset($arguments['ignore_case']) && $arguments['ignore_case']) {
      $ignore_case = TRUE;
      $existing_terms = array_change_key_case($existing_terms);
      foreach ($values as $value) {
        $lower_values[$value] = strtolower($value);
      }
    }
    else {
      $ignore_case = FALSE;
    }
    foreach ($values as $value) {
      if (isset($existing_terms[$value])) {
        $tids[] = $existing_terms[$value];
      }
      elseif ($ignore_case && isset($existing_terms[$lower_values[$value]])) {
        $tids[] = $existing_terms[$lower_values[$value]];
      }
      elseif (!empty($arguments['create_term'])) {
        $new_term = new stdClass();
        $new_term->vid = $vid;
        $new_term->name = $value;
        $new_term->vocabulary_machine_name = $vocab_name;

        // This term is being created with no fields, but we should still call
        // field_attach_validate() before saving, as that invokes
        // hook_field_attach_validate().
        MigrateDestinationEntity::fieldAttachValidate('taxonomy_term', $new_term);
        taxonomy_term_save($new_term);
        $tids[] = $new_term->tid;

        // Add newly created term to existing array.
        $existing_terms[$value] = $new_term->tid;
      }
      else {

        // No term is found for the source value and none is set to be
        // created: warn that data has not been imported.
        $migration = Migration::currentMigration();
        $migration
          ->saveMessage(t("No matching taxonomy term found for source value '@value' in vocabulary %vocab.", array(
          '@value' => $value,
          '%vocab' => $names[$vocab_name]->name,
        )), MigrationBase::MESSAGE_INFORMATIONAL);
      }
    }
  }
  $language = $this
    ->getFieldLanguage($entity, $field_info, $arguments);
  $result = array();
  $delta = 0;
  foreach ($tids as $tid) {
    if (is_array($language)) {
      $current_language = $language[$delta];
    }
    else {
      $current_language = $language;
    }
    $result[$current_language][] = array(
      'tid' => $tid,
    );
    $delta++;
  }
  return $result;
}