You are here

function content_taxonomy_feeds_set_target in Feeds 6

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

Parameters

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

$vid: The selected content_taxonomy CCK field.

$terms: Given terms as array. If a string is used, it is converted to an array using <code>taxonomy_terms_parse_string($terms)->tids</code>.

See also

taxonomy_terms_parse_string().

1 string reference to 'content_taxonomy_feeds_set_target'
content_taxonomy_feeds_node_processor_targets_alter in mappers/content_taxonomy.inc
Implementation of hook_feeds_node_processor_targets_alter().

File

mappers/content_taxonomy.inc, line 47
Enable the user of feeds to map taxonomy terms from the feed for the current node to content_taxonomy CCK fields.

Code

function content_taxonomy_feeds_set_target(&$node, $field_name, $terms) {
  static $fields = array();
  $field = content_fields($field_name, $node->type);

  // Parse string for multiple tags (comma separated)
  if (is_string($terms)) {
    $terms = explode(',', $terms);
  }

  // Return if there are no or empty terms.
  if (!is_array($terms) || empty($terms)) {
    return;
  }
  $tags = $field['widget']['type'] == 'content_taxonomy_autocomplete' && $field['widget']['new_terms'];
  $multiple = $field['widget']['multiple'];
  foreach ($terms as $k => $term_name) {
    $term_name = trim($term_name);
    if ($terms_found = content_taxonomy_get_term_by_name_vid($term_name, $field['vid'])) {

      // If any terms are found add them to the field by found tid.
      foreach ($terms_found as $term_found) {
        if (!is_array($node->{$field_name})) {
          $node->{$field_name} = array();
        }
        array_push($node->{$field_name}, array(
          'value' => $term_found['tid'],
        ));
        if ($multiple != 0 && count($node->{$field_name}) >= $multiple) {

          // If the vocab is not for multiple tags break after the first hit.
          break;
        }
      }
    }
    elseif ($tags) {

      // If the field is configured for free tagging, create a new term
      $edit = array(
        'vid' => $field['vid'],
        'name' => $term_name,
      );
      if ($field['widget']['extra_parent']) {
        $edit['parent'] = $field['widget']['extra_parent'];
      }
      taxonomy_save_term($edit);
      if (!is_array($node->{$field_name})) {
        $node->{$field_name} = array();
      }
      array_push($node->{$field_name}, array(
        'value' => $edit['tid'],
      ));
    }
    if ($multiple != 0 && count($node->{$field_name}) >= $multiple) {

      // If the vocab is not for multiple tags break after a first term has been added.
      break;
    }
  }
}