You are here

function term_reference_filter_by_views_taxonomy_autocomplete in Taxonomy Term Reference Filter by Views 7.2

Same name and namespace in other branches
  1. 7 term_reference_filter_by_views.module \term_reference_filter_by_views_taxonomy_autocomplete()

Autocomplete callback for widgets that has autocomplete.

1 string reference to 'term_reference_filter_by_views_taxonomy_autocomplete'
term_reference_filter_by_views_menu in ./term_reference_filter_by_views.module
Implements hook_menu().

File

./term_reference_filter_by_views.module, line 222

Code

function term_reference_filter_by_views_taxonomy_autocomplete($field_name, $entity_type, $bundle_name, $tid = '', $string = '') {
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  $matches = array();
  $type = 'tags';
  if (!$field || !$instance || $field['type'] != 'taxonomy_term_reference' || !field_access('edit', $field, $entity_type)) {
    return MENU_ACCESS_DENIED;
  }
  $term = NULL;
  if ($tid !== 'NULL' && is_numeric($tid)) {
    $term = entity_load_single('taxonomy_term', $tid);
    if (!$term || !entity_access('view', 'taxonomy_term', $term)) {
      return MENU_ACCESS_DENIED;
    }
  }

  //$handler = entityreference_get_selection_handler($field, $instance, 'taxonomy_term_reference', $term);
  if ($type == 'tags') {

    // The user enters a comma-separated list of tags. We only autocomplete the last tag.
    $tags_typed = drupal_explode_tags($string);
    $tag_last = drupal_strtolower(array_pop($tags_typed));
    if (!empty($tag_last)) {
      $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
    }
  }
  else {

    // The user enters a single tag.
    $prefix = '';
    $tag_last = $string;
  }
  if (!empty($tag_last)) {

    // Get an array of matching entities.
    $term_labels = _get_referencable_terms_from_view($field, $instance, $tag_last, 15, NULL, FALSE);

    // Loop through the products and convert them into autocomplete output.
    foreach ($term_labels as $tid => $label) {
      $key = "{$label} (id:{$tid})";

      // Names containing commas or quotes must be wrapped in quotes.
      if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
        $key = '"' . str_replace('"', '""', $key) . '"';
      }

      //$matches[$prefix . $key] = '<div class="reference-autocomplete">' . $key . '</div>';
      $matches[$prefix . $key] = $key;
    }
  }
  drupal_json_output($matches);
}