You are here

function autocomplete_deluxe_taxonomy_callback in Autocomplete Deluxe 7.2

Custom taxonomy callback, which also accepts an empty string search.

1 string reference to 'autocomplete_deluxe_taxonomy_callback'
autocomplete_deluxe_menu in ./autocomplete_deluxe.module
Implements hook_menu().

File

./autocomplete_deluxe.module, line 30
Define enhanced autocomplete wdiget.

Code

function autocomplete_deluxe_taxonomy_callback($field_name, $tags_typed = '', $limit = 10) {
  $field = field_info_field($field_name);
  $use_synonyms = !empty($_GET['synonyms']);

  // The user enters a comma-separated list of tags. We only autocomplete the
  // last tag.
  $tags_typed = drupal_explode_tags(urldecode($tags_typed));
  $tag_last = drupal_strtolower(array_pop($tags_typed));

  // Initiate tag collections.
  $tags_return = $beginning_tags = $middle_tags = array();

  // Part of the criteria for the query come from the field's own settings.
  $vids = array();
  $vocabularies = taxonomy_vocabulary_get_names();
  foreach ($field['settings']['allowed_values'] as $tree) {

    // If the content taxonomy setting content_taxonomy_ignore_in_suggestions
    // is set, then the vocabulary is ignored.
    if (empty($tree['content_taxonomy_ignore_in_suggestions'])) {
      $vids[] = $vocabularies[$tree['vocabulary']]->vid;
    }
  }
  $query = db_select('taxonomy_term_data', 't');
  $query
    ->addTag('translatable');
  $query
    ->addTag('term_access');
  if (module_exists('synonyms') && !empty($use_synonyms)) {
    $query
      ->leftJoin('field_data_synonyms_synonym', 'fdss', 'fdss.entity_id = t.tid');
  }
  if ($tag_last != '') {

    // Do not select already entered terms.
    if (!empty($tags_typed)) {
      $query
        ->condition('t.name', $tags_typed, 'NOT IN');
    }

    // Select rows that match by term name.
    $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->condition('t.vid', $vids);

    // Clone query, because we need the current state for the "middle" query.
    $middle_query = clone $query;

    // Apply general limit.
    if (isset($limit) && $limit > 0) {
      $query
        ->range(0, $limit);
    }

    // Check for terms beginning with the given term first.
    if (module_exists('synonyms') && !empty($use_synonyms)) {
      $or = db_or();
      $or
        ->condition('fdss.synonyms_synonym_value', db_like($tag_last) . '%', 'LIKE');
      $or
        ->condition('t.name', db_like($tag_last) . '%', 'LIKE');
      $query
        ->condition($or);
    }
    else {
      $query
        ->condition('t.name', db_like($tag_last) . '%', 'LIKE');
    }
    $beginning_tags = $query
      ->execute()
      ->fetchAllKeyed();

    // Check for terms having the term somewhere in it second.
    // Apply new limit, which is the general one minus the results of the
    // beginning query.
    $middle_limit = $limit - count($beginning_tags);

    // Only proceed, if we really need more results.
    if ($middle_limit > 0) {

      // Apply limit.
      $middle_query
        ->range(0, $middle_limit);

      // Make sure we are not getting the same results as for the query above.
      if (!empty($beginning_tags)) {
        $beginning_term_ids = array_keys($beginning_tags);
        $middle_query
          ->condition('t.tid', $beginning_term_ids, 'NOT IN');
      }
      if (module_exists('synonyms') && !empty($use_synonyms)) {
        $or = db_or();
        $or
          ->condition('fdss.synonyms_synonym_value', '%' . db_like($tag_last) . '%', 'LIKE');
        $or
          ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE');
        $middle_query
          ->condition($or);
      }
      else {
        $middle_query
          ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE');
      }
      $middle_tags = $middle_query
        ->execute()
        ->fetchAllKeyed();
    }

    // Build tags.
    $tags_return = $beginning_tags + $middle_tags;
  }
  else {
    $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->condition('t.vid', $vids);
    if (isset($limit) && $limit > 0) {
      $query
        ->range(0, $limit);
    }
    $tags_return = $query
      ->execute()
      ->fetchAllKeyed();
  }
  $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  $term_matches = array();
  foreach ($tags_return as $tid => $name) {
    $n = $name;

    // Term names containing commas or quotes must be wrapped in quotes.
    if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
      $n = '"' . str_replace('"', '""', $name) . '"';
    }
    $term_matches[$prefix . $n] = check_plain($name);
  }
  drupal_json_output($term_matches);
}