index)) { $this->index = search_api_index_load($this->index_id); if (!$this->index) { $this->index = FALSE; } } return $this->index; } /** * @return SearchApiServer * The server this search would at the moment be executed on. */ public function server() { if (!isset($this->server)) { if (!$this->index() || !$this->index()->server) { $this->server = FALSE; } else { $this->server = $this->index()->server(); if (!$this->server) { $this->server = FALSE; } } } return $this->server; } /** * @return boolean * TRUE if the server this search is currently associated with supports the * autocompletion feature; FALSE otherwise. */ public function supportsAutocompletion() { return $this->server() && $this->server()->supportsFeature('search_api_autocomplete'); } /** * Helper method for altering a textfield form element to use autocompletion. */ public function alterElement(array &$element, array $fields = array()) { if (search_api_autocomplete_access($this)) { $fields_string = $fields ? implode(' ', $fields) : ' '; $element['#attached']['css'][] = drupal_get_path('module', 'search_api_autocomplete') . '/search_api_autocomplete.css'; $element['#autocomplete_path'] = 'search_api_autocomplete/' . $this->machine_name . '/' . $fields_string; } } /** * Split a string with search keywords into two parts. * * The first part consists of all words the user has typed completely, the * second one contains the beginning of the last, possibly incomplete word. * * @return array * An array with $keys split into exactly two parts, both of which may be * empty. */ public function splitKeys($keys) { $keys = ltrim($keys); // If there is whitespace or a quote on the right, all words have been // completed. if (rtrim($keys, " \t\n\r\0\x0B\"") != $keys) { return array(rtrim($keys), ''); } if (preg_match('/^(.*?)\s*"?([\S]*)$/', $keys, $m)) { return array($m[1], $m[2]); } return array('', $keys); } /** * Create the query that would be issued for this search for the complete keys. * * @param $complete * A string containing the complete search keys. * @param $incomplete * A string containing the incomplete last search key. * * @return SearchApiQueryInterface * The query that would normally be executed when only $complete was entered * as the search keys for this search. */ public function getQuery($complete, $incomplete) { $info = search_api_autocomplete_get_types($this->type); if (empty($info['create query'])) { return NULL; } $query = $info['create query']($this, $complete, $incomplete); if ($complete && !$query->getKeys()) { $query->keys($complete); } return $query; } }