You are here

public function MigrateTaxonomyTermReferenceMachineNameFieldHandler::prepare in Taxonomy Machine Name 7

Prepare destination field.

Parameters

stdclass $entity: Entitiy.

array $field_info: Field info.

array $instance: Field instance.

array $values: Values.

Return value

array Result.

Throws

\FieldValidationException

Overrides MigrateTaxonomyTermReferenceFieldHandler::prepare

File

./taxonomy_machine_name.migrate.inc, line 285
Taxonomy Machine Name Migrate Module File.

Class

MigrateTaxonomyTermReferenceMachineNameFieldHandler
Class MigrateTaxonomyTermReferenceMachineNameFieldHandler

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 (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);

    // Clean machine names if not matching default rule naming.
    if (!empty($arguments['machine_name'])) {
      $values = array_map('taxonomy_machine_name_clean_name', $values);
    }

    // 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.
    $field_name = empty($arguments['machine_name']) ? 'name' : 'machine_name';
    $existing_terms = db_select('taxonomy_term_data', 'td')
      ->fields('td', array(
      'tid',
      $field_name,
    ))
      ->condition('td.' . $field_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;
        if (!empty($arguments['machine_name'])) {
          $new_term->machine_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().
        field_attach_validate('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;
}