You are here

function _node_import_taxonomy_get_term in Node import 5

Return a tid for a term (text).

Returns 0 if the term will be added. Returns $tid > 0 if the term exists in this vocabulary. Returns -1 if the term does not exist and will not be added.

1 call to _node_import_taxonomy_get_term()
taxonomy_node_import_prepare in supported/taxonomy.inc
Implementation of hook_node_import_prepare().

File

supported/taxonomy.inc, line 143

Code

function _node_import_taxonomy_get_term($vocab, $text, $handler, $preview) {
  static $missing_terms = array();
  $vid = $vocab->vid;
  if (!isset($missing_terms[$vid])) {
    $missing_terms[$vid] = array();
  }

  // Bail out for empty text.
  if (empty($text)) {
    return -1;
  }

  // If we have found this $text already, return it.
  if (isset($missing_terms[$vid][$text])) {
    return $missing_terms[$vid][$text];
  }

  // Try to find a term with a matching name.
  $possibilities = taxonomy_get_term_by_name($text);
  foreach ($possibilities as $possibility) {
    if ($possibility->vid == $vid) {
      $missing_terms[$vid][$text] = $possibility->tid;
      return $possibility->tid;
    }
  }

  // Try to find a synonym
  $possibility = taxonomy_get_synonym_root($text);
  if ($possibility->vid == $vid) {
    $missing_terms[$vid][$text] = $possibility->tid;
    return $possibility->tid;
  }

  // Try to find a term with a matching tid.
  if (is_numeric($text) && ($term = taxonomy_get_term($text))) {
    $missing_terms[$vid][$text] = $term->tid;
    return $term->tid;
  }

  // If we arrive here, the term does not exist.
  switch ($handler) {
    case 'add':
      if ($preview) {
        drupal_set_message(t('Will add %term term to the %name vocabulary.', array(
          '%term' => $text,
          '%name' => $vocab->name,
        )));
        $tid = 0;
      }
      else {
        $edit = array(
          'vid' => $vid,
          'name' => $text,
        );
        $status = taxonomy_save_term($edit);
        $tid = $edit['tid'];
        drupal_set_message(t('Added %term term to the %name vocabulary.', array(
          '%term' => $text,
          '%name' => $vocab->name,
        )));
      }
      break;
    case 'warn':
      drupal_set_message(t('There is no %term term inside the %name vocabulary.', array(
        '%term' => $text,
        '%name' => $vocab->name,
      )));

    //Fall-through
    default:

      // which includes 'ignore' and 'no-import'
      $tid = -1;
      break;
  }
  $missing_terms[$vid][$text] = $tid;
  return $tid;
}