You are here

function node_import_check_taxonomy_term in Node import 6

Check if the value is a valid taxonomy term (by tid or name).

Uses: $field['vocabulary'].

Related topics

1 call to node_import_check_taxonomy_term()
node_import_create_taxonomy in supported/taxonomy.inc
Create a new vocabulary or term by submitting the corresponding form.
1 string reference to 'node_import_check_taxonomy_term'
taxonomy_node_import_fields_alter in supported/taxonomy.inc
Implementation of hook_node_import_fields_alter().

File

supported/taxonomy.inc, line 343
Support file for the core taxonomy module.

Code

function node_import_check_taxonomy_term(&$value, $field, $options, $preview, $op = 'lookup') {
  static $tids;
  $vocab = $field['vocabulary'];
  if ($vocab->tags) {

    // No need to check when tags vocabulary.
    return TRUE;
  }

  // We cache the terms already looked up.
  if (!isset($tids)) {
    $tids = array();
  }
  if (!isset($tids[$vocab->vid])) {
    $tids[$vocab->vid] = array();
  }
  $store_value = is_array($value) ? implode("\n", array_map('drupal_strtolower', $value)) : drupal_strtolower($value);

  // Special case for previews: signify that the term will be created.
  if ($op == 'add') {
    $tids[$vocab->vid][$store_value] = 0;
    return;
  }
  if (isset($tids[$vocab->vid][$store_value])) {

    // We have looked up this value already.
    $value = $tids[$vocab->vid][$store_value];
  }
  else {
    if (!is_array($value)) {

      // One term specified, look up the tid or give error.
      if (($tid = db_result(db_query("SELECT tid FROM {term_data} WHERE vid = %d AND (tid = %d OR LOWER(name) = '%s')", $vocab->vid, is_numeric($store_value) && intval($store_value) > 0 ? $store_value : -1, $store_value))) || ($tid = db_result(db_query("SELECT td.tid FROM {term_synonym} AS ts, {term_data} AS td WHERE td.tid = ts.tid AND td.vid = %d AND LOWER(ts.name) = '%s'", $vocab->vid, $store_value)))) {
        $value = $tid;
      }
      else {
        $value = '';
      }
    }
    else {
      if (count($value) == 1) {

        // The user has specified the term as a hierarchy trail, but he
        // only specified one parent. Lookup that value.
        $value = array_shift($value);
        node_import_check_taxonomy_term($value, $field, $options, $preview);
      }
      else {

        // The user has specified the term as a hierarchy trail of terms.
        // Example: array(grandparent, parent, child). We need to find
        // the tid of the child.
        //
        // Pop child.
        // Find a parent_tid for array(grandparent, parent).
        // Find a tid for child that has a parent with parent_tid.
        $child = array_pop($value);
        $child = drupal_strtolower($child);
        node_import_check_taxonomy_term($value, $field, $options, $preview);
        if ($value === 0 || $value === '') {

          // We can't look for a proper child as we did not find a proper
          // parent (or the parent will be created). So stop.
        }
        else {
          if (($tid = db_result(db_query("SELECT td.tid FROM {term_data} td, {term_hierarchy} th WHERE td.vid = %d AND th.parent = %d AND td.tid = th.tid AND (td.tid = %d OR LOWER(td.name) = '%s')", $vocab->vid, $value, is_numeric($child) && intval($child) > 0 ? $child : -1, $child))) || ($tid = db_result(db_query("SELECT td.tid FROM {term_data} td, {term_hierarchy} th, {term_synonym} ts WHERE td.vid = %d AND th.parent = %d AND td.tid = th.tid AND td.tid = ts.tid AND LOWER(ts.name) = '%s'", $vocab->vid, $value, $child)))) {
            $value = $tid;
          }
          else {
            $value = '';
          }
        }
      }
    }
  }
  $tids[$vocab->vid][$store_value] = $value;

  // Report error.
  if ($value === 0) {
    drupal_set_message(t('The term %value will be added to %vocabulary during import and will be used for %name.', array(
      '%value' => $value,
      '%name' => $field['title'],
      '%vocabulary' => $vocab->name,
    )));
  }
  else {
    if ($value === '') {
      node_import_input_error(t('Input error: %value is not allowed for %name (not a term or synonym in %vocabulary).', array(
        '%value' => $value,
        '%name' => $field['title'],
        '%vocabulary' => $vocab->name,
      )));
      return FALSE;
    }
  }
  return TRUE;
}