You are here

function finder_autocomplete_autocomplete in Finder 6

Same name and namespace in other branches
  1. 7.2 plugins/element_handler/autocomplete.inc \finder_autocomplete_autocomplete()
  2. 7 modules/finder_autocomplete/finder_autocomplete.module \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
Implementation of hook_menu().

File

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

Code

function finder_autocomplete_autocomplete($finder_id, $finder_element_id, $keywords = '') {

  // If the request has a '/' in the search text, then the menu system will have
  // split it into multiple arguments, recover the intended $keywords.
  $args = func_get_args();

  // Shift off the $finder_id argument.
  array_shift($args);

  // Shift off the $finder_element_id argument.
  array_shift($args);
  $keywords = implode('/', $args);
  $choices = array();
  if ($keywords === '') {
    drupal_json($choices);
  }
  $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'])) {
    $keywords = array(
      $keywords,
    );
    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 => array(
      $keywords,
    ),
  ), 'choices', $match, $pager);
  if ($options) {
    foreach ($options as $option) {
      $autofill = theme('finder_autocomplete_autofill', $option, $element);
      $suggestion = theme('finder_autocomplete_suggestion', $option, $element);
      if ($autofill && $suggestion) {
        $choices[$autofill] = $suggestion;
      }
    }
  }
  if (!empty($element->settings['choices']['sort'])) {
    natcasesort($choices);
  }
  drupal_json($choices);
}