You are here

function content_taxonomy_et_autocomplete_deluxe in Content Taxonomy 7

Replacement for taxonomy_autocomplete_deluxe().

Adds an additional left join to the translatable title field.

@todo Merge with content_taxonomy_et_autocomplete().

1 string reference to 'content_taxonomy_et_autocomplete_deluxe'
content_taxonomy_et_menu_alter in ./content_taxonomy_et.module
Implements hook_menu_alter().

File

./content_taxonomy_et.module, line 285
Entity Translation integration for taxonomy terms.

Code

function content_taxonomy_et_autocomplete_deluxe($field_name, $tags_typed = '', $limit = 10) {
  global $language;
  $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($tags_typed);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  $matches = 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');
  $query
    ->leftJoin('field_data_name_field', 'fd', 'fd.entity_id = t.tid AND fd.entity_type = :type AND fd.language = :language', array(
    ':type' => 'taxonomy_term',
    ':language' => $language->language,
  ));
  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',
    ))
      ->fields('fd', array(
      'name_field_value',
    ))
      ->condition('t.vid', $vids);
    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');
      $or
        ->condition('fd.name_field_value', '%' . db_like($tag_last) . '%', 'LIKE');
      $query
        ->condition($or);
    }
    else {
      $query
        ->condition(db_or()
        ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
        ->condition('fd.name_field_value', '%' . db_like($tag_last) . '%', 'LIKE'));
    }
    if (isset($limit) && $limit > 0) {
      $query
        ->range(0, $limit);
    }
    $tags_return = $query
      ->execute();
  }
  else {
    $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->fields('fd', array(
      'name_field_value',
    ))
      ->condition('t.vid', $vids);
    if (isset($limit) && $limit > 0) {
      $query
        ->range(0, $limit);
    }
    $tags_return = $query
      ->execute();
  }
  $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  $term_matches = array();
  foreach ($tags_return as $record) {
    $name = !empty($record->name_field_value) ? $record->name_field_value : $record->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);
}