supportsAutocompletion()) { $server = $search->server(); list($complete, $incomplete) = $search->splitKeys($keys); $keys = preg_replace('/\s+/', ' ', trim($keys)); $query = $search->getQuery($complete, $incomplete); if ($query) { // @todo Maybe make range configurable? $query->range(0, 10); $query->setOption('search id', 'search_api_autocomplete:' . $search->machine_name); if (!empty($search->options['fields'])) { $query->fields($search->options['fields']); } elseif (trim($fields)) { $fields = explode(' ', $fields); $query->fields($fields); } $query->preExecute(); $suggestions = $server->getAutocompleteSuggestions($query, $search, $incomplete, $keys); if ($suggestions) { foreach ($suggestions as $suggestion) { // Convert suggestion strings into an array. if (is_string($suggestion)) { $pos = strpos($suggestion, $keys); if ($pos === FALSE) { $suggestion = array( 'user_input' => '', 'suggestion_suffix' => $suggestion, ); } else { $suggestion = array( 'suggestion_prefix' => substr($suggestion, 0, $pos), 'user_input' => $keys, 'suggestion_suffix' => substr($suggestion, $pos + strlen($keys)), ); } } // Add defaults. $suggestion += array( 'prefix' => NULL, 'suggestion_prefix' => '', 'user_input' => $keys, 'suggestion_suffix' => '', 'results' => NULL, ); if (empty($search->options['results'])) { unset($suggestion['results']); } $key = $suggestion['suggestion_prefix'] . $suggestion['user_input'] . $suggestion['suggestion_suffix']; if (!isset($ret[$key])) { $ret[$key] = theme('search_api_autocomplete_suggestion', $suggestion); } } } } } drupal_json_output($ret); } /** * * * @param array $variables * An associative array containing: * - prefix: For special suggestions, some kind of prefix describing them. * - suggestion_prefix: A suggested prefix for the entered input. * - user_input: The input entered by the user. * - suggestion_suffix: A suggested suffix for the entered input. * - results: If available, the estimated number of results for these keys. */ function theme_search_api_autocomplete_suggestion(array $variables) { extract($variables); $output = ''; if ($prefix) { $output .= "$prefix "; } if ($suggestion_prefix) { $output .= "$suggestion_prefix"; } if ($user_input) { $output .= "$user_input"; } if ($suggestion_suffix) { $output .= "$suggestion_suffix"; } if ($results) { $output .= " $results"; } return "
$output
"; }