You are here

function finder_autocomplete_autocomplete in Finder 7.2

Same name and namespace in other branches
  1. 6 modules/finder_autocomplete/finder_autocomplete.module \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'
autocomplete.inc in plugins/element_handler/autocomplete.inc
The finder autocomplete element handler plugin.

File

plugins/element_handler/autocomplete.inc, line 337
The finder autocomplete element handler plugin.

Code

function finder_autocomplete_autocomplete($finder_name, $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_name argument.
  array_shift($args);

  // Shift off the $element_id argument.
  array_shift($args);
  $keywords = implode('/', $args);
  $choices = array();
  if ($keywords === '') {
    drupal_json_output($choices);
  }
  $keywords = array(
    $keywords,
  );
  $finder = finder_load($finder_name);
  $finder
    ->build();
  $element =& $finder->elements[$element_id];
  if ($finder
    ->esetting($element, 'autocomplete_delimit')) {
    foreach ($keywords as $k => $v) {
      unset($keywords[$k]);
      $exploded = explode($finder
        ->esetting($element, 'autocomplete_delimit'), $v);
      foreach ($exploded as $e) {
        $keywords[] = trim($e);
      }
    }
  }
  $finder->find = array(
    'mode' => 'choices',
    'matches' => array(
      $element->id => array(
        'match' => $finder
          ->esetting($element, 'autocomplete_match'),
        'match_x' => array(
          'operator' => $finder
            ->esetting($finder->elements[$element_id], 'autocomplete_match_custom_operator', '='),
          'value_prefix' => $finder
            ->esetting($finder->elements[$element_id], 'autocomplete_match_custom_prefix'),
          'value_suffix' => $finder
            ->esetting($finder->elements[$element_id], 'autocomplete_match_custom_suffix'),
        ),
      ),
    ),
    'field_logic' => $finder
      ->esetting($element, 'autocomplete_field_logic'),
    'value_logic' => $finder
      ->esetting($element, 'autocomplete_value_logic'),
    'nesting_order' => $finder
      ->esetting($element, 'autocomplete_nesting_order'),
    'pager' => $finder
      ->esetting($element, 'max_suggestions'),
    'keywords' => array(
      $element->id => $keywords,
    ),
    'element' => $element,
  );
  $finder
    ->find();
  $choices = !empty($finder->find['results']) ? $finder->find['results'] : array();
  drupal_json_output($choices);
}