You are here

function taxonomy_manager_autocomplete_tags_get_tids in Taxonomy Manager 6.2

Same name and namespace in other branches
  1. 5 taxonomy_manager.module \taxonomy_manager_autocomplete_tags_get_tids()
  2. 6 taxonomy_manager.admin.inc \taxonomy_manager_autocomplete_tags_get_tids()
  3. 7 taxonomy_manager.admin.inc \taxonomy_manager_autocomplete_tags_get_tids()

helper function for getting out of term ids from autocomplete fields non-exsiting terms get inserted autmatically the input gets parsed for term names, optional a term id can be directly passed with prefixing the input with 'term-id:'

Parameters

$typed_input input string of form field:

$vid vocabulary id:

$insert_new TRUE if non-existing terms should be inserted:

$lang define the language (optional):

Return value

array of term ids

6 calls to taxonomy_manager_autocomplete_tags_get_tids()
taxonomy_manager_form_merge_submit in ./taxonomy_manager.admin.inc
Submit handler for merging terms
taxonomy_manager_form_merge_validate in ./taxonomy_manager.admin.inc
Validation handler for validating terms
taxonomy_manager_form_move_submit in ./taxonomy_manager.admin.inc
Submit handler for moving terms
taxonomy_manager_form_move_validate in ./taxonomy_manager.admin.inc
Validation handler for moving terms
taxonomy_manager_term_data_edit in ./taxonomy_manager.admin.inc
callback handler for updating term data

... See full list

File

./taxonomy_manager.admin.inc, line 2254
Taxonomy Manager Admin

Code

function taxonomy_manager_autocomplete_tags_get_tids($typed_input, $vid, $insert_new = TRUE, $lang = NULL) {
  $tids = array();
  $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
  preg_match_all($regexp, $typed_input, $matches);
  $typed_terms = array_unique($matches[1]);
  foreach ($typed_terms as $typed_term) {
    $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\\1', $typed_term));
    $typed_term = trim($typed_term);
    if ($typed_term != "") {
      if (substr($typed_term, 0, 8) == "term-id:") {
        $id = substr($typed_term, 8);
        $term = taxonomy_get_term($id);
        if ($term->vid == $vid) {
          $tids[$term->tid]['tid'] = $term->tid;
        }
      }
      else {
        $possibilities = taxonomy_get_term_by_name($typed_term);
        $typed_term_tid = NULL;

        // tid match if any.
        foreach ($possibilities as $possibility) {
          if ($possibility->vid == $vid && !($lang && $lang != $possibility->language)) {
            $typed_term_tid = $possibility->tid;
            $tids[$typed_term_tid]['tid'] = $typed_term_tid;
          }
        }
        if (!$typed_term_tid && $insert_new) {
          $edit = array(
            'vid' => $vid,
            'name' => $typed_term,
          );
          $status = taxonomy_save_term($edit);
          if (module_exists('i18ntaxonomy') && $lang != "") {
            _i18ntaxonomy_term_set_lang($edit['tid'], $lang);
          }
          $typed_term_tid = $edit['tid'];
          $tids[$typed_term_tid]['tid'] = $typed_term_tid;
          $tids[$typed_term_tid]['new'] = TRUE;
        }
      }
    }
  }
  return $tids;
}