You are here

function taxonomy_feeds_set_target in Feeds 6

Same name and namespace in other branches
  1. 8.2 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.

Parameters

$node: Reference to the node object we are working on.

$key: A key as added to the $targets array by taxonomy_feeds_node_processor_targets_alter().

$terms: Given terms as array.

Add the given terms to the node object so the taxonomy module can add them on node_save().

1 string reference to 'taxonomy_feeds_set_target'
taxonomy_feeds_node_processor_targets_alter in mappers/taxonomy.inc
Implementation of hook_feeds_node_processor_targets_alter().

File

mappers/taxonomy.inc, line 74
Mapper that exposes a node's taxonomy vocabularies as mapping targets.

Code

function taxonomy_feeds_set_target(&$node, $key, $terms) {

  // Return if there are no terms.
  if (empty($terms)) {
    return;
  }

  // Load target vocabulary to check, if it has the "tags" flag.
  $vocabulary = taxonomy_get_vocabulary(str_replace('taxonomy:', '', $key));

  // Cast a given single string to an array so we can use it.
  if (!is_array($terms)) {
    $terms = array(
      $terms,
    );
  }
  if (!isset($node->taxonomy)) {
    $node->taxonomy = array();
  }
  if ($vocabulary->tags) {
    if (!isset($node->taxonomy['tags'])) {
      $node->taxonomy['tags'] = array();
    }
    foreach ($terms as $k => $v) {

      // Make sure there aren't any terms with a comma (=tag delimiter) in it.
      $terms[$k] = preg_replace('/\\s*,\\s*/', ' ', $v);
    }

    // Simply add a comma separated list to the node for a "tags" vocabulary.
    if (isset($node->taxonomy['tags'][$vocabulary->vid])) {
      $terms = array_merge($terms, drupal_explode_tags($node->taxonomy['tags'][$vocabulary->vid]));
    }
    $node->taxonomy['tags'][$vocabulary->vid] = implode(',', $terms);
  }
  else {
    foreach ($terms as $term) {
      if ($term instanceof FeedsTermElement) {
        $node->taxonomy[$term->tid] = (object) $term;
      }
      elseif ($terms_found = taxonomy_get_term_by_name_vid($term, $vocabulary->vid)) {

        // If any terms are found add them to the node's taxonomy by found tid.
        foreach ($terms_found as $term_found) {
          $node->taxonomy[$term_found->tid] = $term_found;
          if (!$vocabulary->multiple) {
            break;
          }
        }
      }

      // If the vocab is not for multiple tags break after the first hit.
      if (!$vocabulary->multiple) {
        break;
      }
    }
  }
}