Adding existing terms to selected vocab fails in _yahoo_terms_create_vocabulary_items

ragaskar - November 26, 2007 - 22:30
Project:Yahoo Terms
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:adnan042
Status:active
Description

_yahoo_terms_create_vocabulary_items will only add existing terms to the given vid for (at most) the FIRST existing term that does NOT belong to the vid:

IE $terms = array('foo','bar','baz'), $vid=2

let's imagine that taxonomy_get_term_by_name($term) will give a result for each term,
but with a vid of 1.

given this, only 'foo' will be added to our vocabulary, because after it is added, the array $tids is non-empty, which means we can no longer satisfy the condition to add existing terms to a new tid.

suggestion: this isn't super-elegant, but adding a flag that gets set to TRUE if we find an existing tid for our vid, rather than relying on the state of the $tids array, will cause the function to behave as intended. Code below

function _yahoo_terms_create_vocabulary_items($keywords, $vid, $static) {
  if ($vid == FALSE) {
    return FALSE;
  }
  $tids = array();
  if (is_array($keywords)) {
    foreach ($keywords as $term) {
      $curr_terms = taxonomy_get_term_by_name($term);
//ADD FLAG
      $existing_term=FALSE;
      if (count($curr_terms) != 0) {
        foreach ($curr_terms as $curr_term) {
          if ($curr_term->vid == $vid) {
//SET FLAG TO TRUE
            $existing_term=TRUE;
            $tids[$curr_term->tid] = $curr_term;
          }
        }
      }
//IF WE DIDN'T FIND AN EXISTING TERM, THEN CREATE ONE.
      if (!$existing_term && $static == FALSE) {
          $new_term['name'] = $term;
          $new_term['vid'] = $vid;
          taxonomy_save_term($new_term);
          $tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
          unset($new_term);
        }
    }
  }
  return $tids;
}

it's likely you could even do without the $existing_term flag and just use a continue 2; statement as well, but i haven't used conditional loop skips for a bit and don't have time to debug such an extravagance right now (but it'd probably look a little smoother).

#1

ragaskar - November 26, 2007 - 22:35

bah, can't leave well enough alone: tested the continue 2; ... looks like it works ok. here's the code rewritten without the flag:

<?php
function _yahoo_terms_create_vocabulary_items($keywords, $vid, $static) {
  if (
$vid == FALSE) {
    return
FALSE;
  }
 
$tids = array();
  if (
is_array($keywords)) {
    foreach (
$keywords as $term) {
     
$curr_terms = taxonomy_get_term_by_name($term);
      if (
count($curr_terms) != 0) {
        foreach (
$curr_terms as $curr_term) {
          if (
$curr_term->vid == $vid) {
           
$tids[$curr_term->tid] = $curr_term;
            continue
2;
           }
        }
      }
      if (
$static == FALSE) {
         
$new_term['name'] = $term;
         
$new_term['vid'] = $vid;
         
taxonomy_save_term($new_term);
         
$tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
          unset(
$new_term);
        }
    }
  }
  return
$tids;
}
?>

#2

adnan042 - October 13, 2008 - 20:59
Assigned to:Anonymous» adnan042
AttachmentSize
n1079802303_6346.jpg 3.17 KB
 
 

Drupal is a registered trademark of Dries Buytaert.