You are here

function taxonomy_feeds_set_target in Feeds 7.2

Same name and namespace in other branches
  1. 8.2 mappers/taxonomy.inc \taxonomy_feeds_set_target()
  2. 6 mappers/taxonomy.inc \taxonomy_feeds_set_target()
  3. 7 mappers/taxonomy.inc \taxonomy_feeds_set_target()

Callback for mapping taxonomy terms.

3 calls to taxonomy_feeds_set_target()
FeedsMapperTaxonomyTestCase::testAllowedVocabularies in tests/feeds_mapper_taxonomy.test
Tests that only term references are added from allowed vocabularies.
FeedsMapperTaxonomyTestCase::testSearchByGUID in tests/feeds_mapper_taxonomy.test
Tests mapping to a taxonomy term's guid.
FeedsMapperTaxonomyTestCase::testSearchByID in tests/feeds_mapper_taxonomy.test
Tests mapping to taxonomy terms by tid.
1 string reference to 'taxonomy_feeds_set_target'
taxonomy_feeds_processor_targets in mappers/taxonomy.inc
Implements hook_feeds_processor_targets().

File

mappers/taxonomy.inc, line 87
On behalf implementation of Feeds mapping API for taxonomy.module.

Code

function taxonomy_feeds_set_target(FeedsSource $source, $entity, $target, array $terms, array $mapping) {
  $language = $mapping['language'];

  // Add in default values.
  $mapping += array(
    'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
    'autocreate' => FALSE,
  );
  $info = field_info_field($target);
  $cache =& drupal_static(__FUNCTION__);
  if (!isset($cache['allowed_values'][$target])) {
    $cache['allowed_values'][$target] = taxonomy_allowed_values($info);
  }
  if (!isset($cache['allowed_vocabularies'][$target])) {
    foreach ($info['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
        $vid = $vocabulary->vid;
        $cache['allowed_vocabularies'][$target][$vid] = $vocabulary->machine_name;
        $cache['vocabulary_targets'][$vid][] = $target;
      }
    }
  }

  // Some kind of configuration issue. Perhaps the vocabulary was deleted.
  // Nothing we can do about it.
  if (empty($cache['allowed_vocabularies'][$target])) {
    return;
  }
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->entityCondition('bundle', $cache['allowed_vocabularies'][$target])
    ->range(0, 1);
  $field = isset($entity->{$target}) ? $entity->{$target} : array(
    $language => array(),
  );
  if (!isset($field[$language])) {
    $field[$language] = array();
  }

  // Allow for multiple mappings to the same target.
  $delta = count($field[$language]);

  // Iterate over all values.
  foreach ($terms as $term) {
    if ($info['cardinality'] == $delta) {
      break;
    }
    $tid = FALSE;

    // FeedsTermElement already is a term.
    if ($term instanceof FeedsTermElement) {
      $tid = $term->tid;
    }
    else {
      switch ($mapping['term_search']) {

        // Lookup by name.
        case FEEDS_TAXONOMY_SEARCH_TERM_NAME:
          $term = trim($term);
          $name_query = clone $query;
          if (strlen($term) && ($tids = $name_query
            ->propertyCondition('name', $term)
            ->execute())) {

            // Find the first allowed term.
            foreach ($tids['taxonomy_term'] as $possible_term) {
              if (isset($cache['allowed_values'][$target][$possible_term->tid])) {
                $tid = $possible_term->tid;
                break;
              }
            }
          }
          elseif ($mapping['autocreate'] && strlen($term)) {
            $term_vid = key($cache['allowed_vocabularies'][$target]);
            $term = (object) array(
              'name' => drupal_substr($term, 0, 255),
              'vid' => $term_vid,
              'vocabulary_machine_name' => reset($cache['allowed_vocabularies'][$target]),
            );

            // Set language if the taxonomy is multilingual.
            if ($language !== LANGUAGE_NONE) {
              $info = entity_get_info('taxonomy_term');
              if (!empty($info['entity keys']['language'])) {
                $term->{$info['entity keys']['language']} = $language;
              }
            }
            taxonomy_term_save($term);
            $tid = $term->tid;

            // Add to the list of allowed values.
            $cache['allowed_values'][$target][$tid] = $term->name;

            // Invalidate caches for other fields targeting the same vocabulary.
            foreach ($cache['vocabulary_targets'][$term_vid] as $clear_target) {
              if ($clear_target !== $target) {
                unset($cache['allowed_values'][$clear_target]);
              }
            }
          }
          break;

        // Lookup by tid.
        case FEEDS_TAXONOMY_SEARCH_TERM_ID:
          if (is_numeric($term)) {
            $tid = (int) $term;
          }
          break;

        // Lookup by GUID.
        case FEEDS_TAXONOMY_SEARCH_TERM_GUID:
          $tid = taxonomy_feeds_term_lookup_term_by_guid($term);
          break;
      }
    }
    if ($tid && isset($cache['allowed_values'][$target][$tid])) {
      $field[$language][] = array(
        'tid' => $tid,
      );
      $delta++;
    }
  }
  $entity->{$target} = $field;
}