You are here

function content_taxonomy_autocomplete_tags_get_tids in Content Taxonomy 5

Same name and namespace in other branches
  1. 6.2 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_tags_get_tids()
  2. 6 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_tags_get_tids()

Get TIDs for freetagging tags Free tagging vocabularies do not send their tids in the form, so we'll detect them here and process them independently.

Parameters

$typed_input A string containing all comma separated tags. As the user typed it.:

1 call to content_taxonomy_autocomplete_tags_get_tids()
content_taxonomy_autocomplete_widget in ./content_taxonomy_autocomplete.module
Implementation of hook_widget().

File

./content_taxonomy_autocomplete.module, line 220
Defines a widget type for content_taxonomy with autocomplete

Code

function content_taxonomy_autocomplete_tags_get_tids($typed_input, $vid, $parent = 0) {

  // This regexp allows the following types of user input:
  // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
  $typed_terms = content_taxonomy_autocomplete_split_tags($typed_input);
  foreach ($typed_terms as $typed_term) {

    // If a user has escaped a term (to demonstrate that it is a group,
    // or includes a comma or quote character), we remove the escape
    // formatting so to save the term into the DB as the user intends.
    $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\\1', $typed_term));
    $typed_term = trim($typed_term);
    if ($typed_term == "") {
      continue;
    }

    // See if the term exists in the chosen vocabulary
    // and return the tid, otherwise, add a new record.
    $possibilities = taxonomy_get_term_by_name($typed_term);
    $typed_term_tid = NULL;

    // tid match if any.
    foreach ($possibilities as $possibility) {
      if ($possibility->vid == $vid) {
        if ($parent) {
          $parents = array();
          $parents = taxonomy_get_parents($possibility->tid);
          if (in_array($parent, array_keys($parents))) {
            $result['existing_tids'][$possibility->tid] = $possibility->tid;
            $typed_term_tid = $possibility->tid;
          }
        }
        else {
          $result['existing_tids'][$possibility->tid] = $possibility->tid;
          $typed_term_tid = $possibility->tid;
        }
      }
    }
    if (!$typed_term_tid) {
      $result['non_existing_terms'][] = array(
        'name' => $typed_term,
        'vid' => $vid,
      );
    }
  }
  return $result;
}