You are here

function taxonomy_feeds_set_target in Feeds 8.2

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

Callback for mapping. Here is where the actual mapping happens.

@todo Do not create new terms for non-autotag fields.

2 calls to taxonomy_feeds_set_target()
FeedsMapperTaxonomyTest::testSearchByGUID in lib/Drupal/feeds/Tests/FeedsMapperTaxonomyTest.php
Tests mapping to a taxonomy term's guid.
FeedsMapperTaxonomyTest::testSearchByID in lib/Drupal/feeds/Tests/FeedsMapperTaxonomyTest.php
Tests mapping to taxonomy terms by tid.
1 string reference to 'taxonomy_feeds_set_target'
taxonomy_feeds_processor_targets_alter in mappers/taxonomy.inc
Implements hook_feeds_processor_targets_alter().

File

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

Code

function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) {

  // Allow mapping the string '0' to a term name.
  if (empty($terms) && $terms != 0) {
    return;
  }

  // Handle non-multiple values.
  if (!is_array($terms)) {
    $terms = array(
      $terms,
    );
  }

  // 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])) {
    $instance = field_info_instance($entity
      ->entityType(), $target, $entity
      ->bundle());
    $cache['allowed_values'][$target] = taxonomy_allowed_values($info, $instance, $entity);
  }
  if (!isset($cache['allowed_vocabularies'][$target])) {
    foreach ($info['settings']['allowed_values'] as $tree) {
      if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) {
        $cache['allowed_vocabularies'][$target][$vocabulary
          ->id()] = $vocabulary
          ->id();
      }
    }
  }
  $query = Drupal::entityQuery('taxonomy_term');
  $query
    ->condition('vid', $cache['allowed_vocabularies'][$target])
    ->range(0, 1);
  $field = isset($entity->{$target}) ? $entity->{$target} : array(
    'und' => array(),
  );

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

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

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

        // Lookup by name.
        case FEEDS_TAXONOMY_SEARCH_TERM_NAME:
          $name_query = clone $query;
          if ($tids = $name_query
            ->condition('name', $term)
            ->execute()) {
            $tid = key($tids);
          }
          elseif ($mapping['autocreate']) {
            $term = entity_create('taxonomy_term', array(
              'name' => $term,
              'vid' => key($cache['allowed_vocabularies'][$target]),
            ));
            $term
              ->save();
            $tid = $term
              ->id();

            // Add to the list of allowed values.
            $cache['allowed_values'][$target][$tid] = $term
              ->label();
          }
          break;

        // Lookup by tid.
        case FEEDS_TAXONOMY_SEARCH_TERM_ID:
          if (is_numeric($term)) {
            $tid = $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['und'][$delta]['tid'] = $tid;
      $delta++;
    }
  }
  $entity->{$target} = $field;
}