You are here

function finder_autocomplete_autocomplete in Finder 7

Same name and namespace in other branches
  1. 6 modules/finder_autocomplete/finder_autocomplete.module \finder_autocomplete_autocomplete()
  2. 7.2 plugins/element_handler/autocomplete.inc \finder_autocomplete_autocomplete()

Menu callback; get autocomplete suggestions.

1 string reference to 'finder_autocomplete_autocomplete'
finder_autocomplete_menu in modules/finder_autocomplete/finder_autocomplete.module
Implements hook_menu().

File

modules/finder_autocomplete/finder_autocomplete.module, line 164
The finder autocomplete module.

Code

function finder_autocomplete_autocomplete($finder_id, $finder_element_id, $keywords = '') {
  $choices = array();
  if ($keywords === '') {
    drupal_json_output($choices);
  }
  $keywords = array(
    $keywords,
  );
  $finder = finder_load($finder_id);
  $element =& finder_element($finder, $finder_element_id);
  $fields =& $element->settings['choices']['field'];
  foreach ($fields as $key => $field) {
    $field_info[$key] = finder_split_field($field);
    $field_names[$key] = $field_info[$key]['field'];
  }
  $pager =& $element->settings['form']['max_suggestions'];
  $match = $element->settings['form']['match'] ? $element->settings['form']['match'] : 'c';
  if (!empty($element->settings['form']['delimit_autocomplete'])) {
    foreach ($keywords as $k => $v) {
      unset($keywords[$k]);
      $exploded = explode($element->settings['form']['delimit_autocomplete'], $v);
      foreach ($exploded as $e) {
        $keywords[] = trim($e);
      }
    }
  }
  $options = finder_find($finder, array(
    $finder_element_id => $keywords,
  ), 'choices', $match, $pager);
  if ($options) {
    foreach ($options as $option) {
      $autofill = theme('finder_autocomplete_autofill', array(
        'option' => $option,
        'finder_element' => $element,
      ));
      $suggestion = theme('finder_autocomplete_suggestion', array(
        'option' => $option,
        'finder_element' => $element,
      ));
      if ($autofill && $suggestion) {
        $choices[$autofill] = $suggestion;
      }
    }
  }
  drupal_json_output($choices);
}