1

I have a $category variable containing country names separated by comma. So I use the following code to save them as taxonomy terms:

$tids = array(); $ctids = array();
$category = explode(',',$category);
foreach($category as $cat) {
  insert_term_if_not_exists($cat, 'countries');
  $term_country = taxonomy_get_term_by_name($cat);
  $tid_country = key($term_country);
  $lang_country = field_language('node', $node, 'field_country');
  array_push($ctids, $tid_country);
}
foreach ($ctids as $index => $ctid) { 
 $node->field_country[$lang_country][$index]['tid'] = $ctid; 
}

As you see the above code uses the insert_term_if_not_exists() function which works perfectly well to check if a taxonomy term exists and to create it programmatically if it doesn't:

function insert_term_if_not_exists($term_name, $vocabulary_name) {
   $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
   $matched_terms = taxonomy_get_term_by_name($term_name, $vocabulary->machine_name);
  if(empty($matched_terms)) {
    $term = new stdClass();
    $term->vid = $vocabulary->vid;
    $term->name = $term_name;
    taxonomy_term_save($term);
  } else {
    $term = array_shift($matched_terms);
  }
return $term;
}

However, I also need to check and avoid creating a new taxonomy term if it is a synonym of already existing another taxonomy term. So I modified the above code in the following way:

function insert_term_if_not_exists($term_name, $vocabulary_name) {
   $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
   $matched_terms = taxonomy_get_term_by_name($term_name, $vocabulary->machine_name);

    if(empty($matched_terms)) {
      $matched_synonym = synonyms_get_term_by_synonym($term_name, $vocabulary);
      if (empty($matched_synonym)) {
        $term = new stdClass();
        $term->vid = $vocabulary->vid;
        $term->name = $term_name;
        taxonomy_term_save($term);
      } else {
        /*
        * Here I need to do something, but don't know what exactly. Using
        * $term = taxonomy_term_load($matched_synonym);
        * here doesn't work.
        */
        }
    } else {
        $term = array_shift($matched_terms);
    }
  return $term;
}

Having troubles to make it work properly.

3
  • synonyms_get_term_by_synonym() expects its $vocabulary parameter to be a fully loaded vocabulary object, so make sure you're passing a valid vocabulary object. If it isn't, that could be the cause of the the error you get on line 356.
    – dblue
    Feb 25, 2015 at 18:45
  • But what is a "valid vocabulary object"? Both $term_name and $vocabulary->machine_name are just strings which work perfectly well with the insert_term_if_not_exists function given above. So what exactly I need to feed synonyms_get_term_by_synonym() with, what is the difference?
    – Sasha
    Feb 26, 2015 at 9:29
  • I pretty much meant to make sure you're giving it $vocabulary and not $vocabulary->machine_name or $vocabulary_name. Internally that function is using $vocabulary->vid so if $vocabulary is a valid vocabulary object, it'll presumably have a vid.
    – dblue
    Feb 26, 2015 at 18:29

1 Answer 1

0

Edited my answer since my original answer referred to dev code.

The following simple module works with Drupal 7.34 and the current stable version of synonyms (7.x-1.2) and successfully matches a synonym to a taxonomy term.

synonyms_test.module:

function synonyms_test_menu() {
  $menu['test-synonyms'] = array(
    'page callback' => 'synonyms_test_page',
    'access callback' => TRUE,
  );

  return $menu;
}

function synonyms_test_page() {
  $vocabulary_name = 'tags';
  $output = '<p>Assuming you have a taxonomy with machine name "tags" and that '
            . 'taxonomy has a term named "first term" and that term has a '
            . 'synonym "syn one", this test will produce these results:</p>';
  $test_terms = array('first term', 'bogus term', 'syn one');
  $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
  foreach($test_terms as $test_term) {
    $output .= 'Testing "' . $test_term . '"<br>';
    $matched_terms = taxonomy_get_term_by_name($test_term, $vocabulary->machine_name);
    if(empty($matched_terms)) {
      $output .= 'Not an existing taxonomy term.';
    } else {
      $output .= 'Is an existing taxonomy term.';
    }
    $output .= '<br><br>';
    $matched_synonym = synonyms_get_term_by_synonym($test_term, $vocabulary);
    if(empty($matched_synonym)) {
      $output .= 'Not an existing synonym.';
    }
    else {
      $output .= 'Is an existing synonym, for tid ' . $matched_synonym;
    }
    $output .= '<br>=========<br><br>';
  }
  return $output;
}

* Original answer below *

This is the function you're looking for (from synonyms.module):

/**
 * Returns the term objects that have the given string as a synonym.
 *
 * @param $synonym
 *  A string containing the synonym to search for.
 * @param $vocabulary
 *  (optional) The machine name of the vocabulary in which to search for.

 * @return
 *  An array of term objects containing that synonym.
 */
function synonyms_root_terms_get($synonym, $vocabulary = NULL) {
  $tids = synonyms_root_tids_get($synonym, $vocabulary);

  return taxonomy_term_load_multiple($tids);
}

So if your $matched_terms is empty, try something like:

$matched_synonyms = synonyms_root_terms_get($term_name, $vocabulary->machine_name);
if(empty($matched_synonyms)) {
  // ...now create your new term
}
else {
  $term = array_shift($matched_synonyms);
}
11
  • Thanks @dblue, but I tried that before and, unfortunately, it is giving: Fatal error: Call to undefined function synonyms_root_terms_get()
    – Sasha
    Feb 25, 2015 at 8:00
  • I also tried the function synonyms_get_term_by_synonym() with no much success, please see the updated post above.
    – Sasha
    Feb 25, 2015 at 8:04
  • Oh, my mistake, synonyms_root_terms_get() is in the master branch of the module's code, but not in the 7.x-1.x branch, that's why you get the undefined function error.
    – dblue
    Feb 25, 2015 at 18:41
  • I'll give a try to master branch if it works.
    – Sasha
    Feb 26, 2015 at 9:30
  • Since it's dev code, I hope it's stable enough to work, if you can't get synonyms_get_term_by_synonym() in the current release to work. Let us know how it goes.
    – dblue
    Feb 26, 2015 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.