vid] = array( 'name' => "Taxonomy: ".$vocab->name, 'callback' => 'taxonomy_feeds_set_target', 'description' => t('Taxonomy mapping'), ); } } /** * Callback for mapping. Here is where the actual mapping happens. * * When the callback is invoked, $target contains the name of the field the * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ function taxonomy_feeds_set_target(&$node, $targetVocab, $values) { // our $targetVocab is the selected vocabular, which we should search // or create terms in. We only create terms, if this vocabular is a tag-type one $existing_term = NULL; // Load target vocabulary to check, if it has the "tags" flag $vocab = taxonomy_vocabulary_load($targetVocab); //preconditions if(is_array($values) === false || $vocab === false || count($values) == 0) { // TODO: show an error? return false; } // this flag will be used later, to check if we have to craete new terms $new_terms = false; $terms = array(); if($vocab->tags === 0) { // we only the the "term exist" checks if the vocab is not a tag-type // tag-typed vocabs are saved differently and they will be checked for existence // in the taxonomy code... thank you for all the special cases!! foreach($values as $term_name) { // Drupal has no proper callback for getting a term by name for a specific vid // if there is no such term, it returns an empty array $tmp = taxonomy_get_term_by_name_vid($term_name, $vocab->vid); if(count($matching_terms) == 0) { // no existing terms, so lets add the term with his name without a tid $terms[] = (object) array('name' => $term_name); // set the flag, that we have new terms $new_terms = true; } else { $terms[] = $tmp[0]; // we only use the first matched term } } } else { $terms = $values; } if($vocab->tags) { // no matter its an existing Tag or not, they are simply saved as // comma seperated list $node->taxonomy['tags'][$vocab->vid] = implode(',',$terms); } // we are not allowed to add any new terms here, as the vocab is not a "tag-type" one else if($new_terms === false && count($terms) >0) { // we dont need to add new terms, the all exist $node->taxonomy[$vocab->vid] = $terms; } } function taxonomy_get_term_by_name_vid($name,$vid) { $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND vid=%d", 't', 'tid'), trim($name),$vid); $result = array(); while ($term = db_fetch_object($db_result)) { $result[] = $term; } return $result; }