You are here

class MigrateTaxonomyTermReferenceFieldHandler in Migrate 7.2

Hierarchy

Expanded class hierarchy of MigrateTaxonomyTermReferenceFieldHandler

1 string reference to 'MigrateTaxonomyTermReferenceFieldHandler'
migrate_migrate_api in ./migrate.migrate.inc

File

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

View source
class MigrateTaxonomyTermReferenceFieldHandler extends MigrateFieldHandler {
  public function __construct() {
    $this
      ->registerTypes(array(
      'taxonomy_term_reference',
    ));
  }

  /**
   * Implementation of MigrateFieldHandler::fields().
   *
   * @param $type
   *  The field type.
   * @param $instance
   *  Instance info for the field.
   * @param Migration $migration
   *  The migration context for the parent field. We can look at the mappings
   *  and determine which subfields are relevant.
   *
   * @return array
   */
  public function fields($type, $instance, $migration = NULL) {
    return array(
      'source_type' => t('Option: <a href="@doc">Set to \'tid\' when the value is a source ID</a>', array(
        '@doc' => 'http://drupal.org/node/1224042#source_type',
      )),
      'create_term' => t('Option: <a href="@doc">Set to TRUE to create referenced terms when necessary</a>', array(
        '@doc' => 'http://drupal.org/node/1224042#create_term',
      )),
      'ignore_case' => t('Option: <a href="@doc">Set to TRUE to ignore case differences between source data and existing term names</a>', array(
        '@doc' => 'http://drupal.org/node/1224042#ignore_case',
      )),
      'not_empty' => t('Option: Set to TRUE to ignore empty terms to get created in case of multiple term names per row.'),
    );
  }
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateFieldHandler::getFieldLanguage function Determine the language of the field.
MigrateHandler::$dependencies protected property List of other handler classes which should be invoked before the current one.
MigrateHandler::$typesHandled protected property List of "types" handled by this handler. Depending on the kind of handler, these may be destination types, field types, etc.
MigrateHandler::getDependencies public function
MigrateHandler::getTypesHandled public function
MigrateHandler::handlesType public function Does this handler handle the given type? 1
MigrateHandler::registerTypes protected function Register a list of types handled by this class
MigrateTaxonomyTermReferenceFieldHandler::fields public function Implementation of MigrateFieldHandler::fields().
MigrateTaxonomyTermReferenceFieldHandler::prepare public function
MigrateTaxonomyTermReferenceFieldHandler::__construct public function Overrides MigrateHandler::__construct