You are here

class SearchApiAutocompleteServerSuggester in Search API Autocomplete 7

Provides a suggester plugin that retrieves suggestions from the server.

The server needs to support the "search_api_autocomplete" feature for this to work.

Hierarchy

Expanded class hierarchy of SearchApiAutocompleteServerSuggester

2 string references to 'SearchApiAutocompleteServerSuggester'
hook_search_api_autocomplete_suggester_info in ./search_api_autocomplete.api.php
Add new plugins for calculating autocomplete suggestions.
search_api_autocomplete_search_api_autocomplete_suggester_info in ./search_api_autocomplete.module
Implements hook_search_api_autocomplete_suggester_info().

File

src/SearchApiAutocompleteServerSuggester.php, line 14
Contains SearchApiAutocompleteServerSuggester.

View source
class SearchApiAutocompleteServerSuggester extends SearchApiAutocompleteSuggesterPluginBase {

  /**
   * {@inheritdoc}
   */
  public static function supportsIndex(SearchApiIndex $index) {
    try {
      return $index
        ->server() && $index
        ->server()
        ->supportsFeature('search_api_autocomplete');
    } catch (SearchApiException $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array(
      'fields' => array(),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, array &$form_state) {

    // Add a list of fields to include for autocomplete searches.
    $search = $this
      ->getSearch();
    $fields = $search
      ->index()
      ->getFields();
    $fulltext_fields = $search
      ->index()
      ->getFulltextFields();
    $options = array();
    foreach ($fulltext_fields as $field) {
      $options[$field] = check_plain($fields[$field]['name']);
    }
    $form['fields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Override used fields'),
      '#description' => t('Select the fields which should be searched for matches when looking for autocompletion suggestions. Leave blank to use the same fields as the underlying search.'),
      '#options' => $options,
      '#default_value' => drupal_map_assoc($this->configuration['fields']),
      '#attributes' => array(
        'class' => array(
          'search-api-checkboxes-list',
        ),
      ),
    );
    $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array $form, array &$form_state) {
    $values = $form_state['values'];
    $values['fields'] = array_keys(array_filter($values['fields']));
    $this
      ->setConfiguration($values);
  }

  /**
   * {@inheritdoc}
   */
  public function getAutocompleteSuggestions(SearchApiQueryInterface $query, $incomplete_key, $user_input) {
    if ($this->configuration['fields']) {
      $query
        ->fields($this->configuration['fields']);
    }
    $query
      ->preExecute();
    return $query
      ->getIndex()
      ->server()
      ->getAutocompleteSuggestions($query, $this
      ->getSearch(), $incomplete_key, $user_input);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAutocompleteServerSuggester::buildConfigurationForm public function Constructs the plugin's configuration form. Overrides SearchApiAutocompleteSuggesterPluginBase::buildConfigurationForm
SearchApiAutocompleteServerSuggester::defaultConfiguration public function Retrieves the default configuration for this plugin. Overrides SearchApiAutocompleteSuggesterPluginBase::defaultConfiguration
SearchApiAutocompleteServerSuggester::getAutocompleteSuggestions public function Retrieves autocompletion suggestions for some user input. Overrides SearchApiAutocompleteSuggesterInterface::getAutocompleteSuggestions
SearchApiAutocompleteServerSuggester::submitConfigurationForm public function Submits the plugin's configuration form. Overrides SearchApiAutocompleteSuggesterPluginBase::submitConfigurationForm
SearchApiAutocompleteServerSuggester::supportsIndex public static function Determines whether this plugin class supports the given index. Overrides SearchApiAutocompleteSuggesterPluginBase::supportsIndex
SearchApiAutocompleteSuggesterPluginBase::$configuration protected property The suggester plugin's configuration.
SearchApiAutocompleteSuggesterPluginBase::$pluginDefinition protected property The suggester plugin's definition.
SearchApiAutocompleteSuggesterPluginBase::$pluginId protected property The suggester plugin's ID.
SearchApiAutocompleteSuggesterPluginBase::$search protected property The search this suggester is attached to.
SearchApiAutocompleteSuggesterPluginBase::create public static function Creates a new instance of this class. Overrides SearchApiAutocompleteSuggesterInterface::create
SearchApiAutocompleteSuggesterPluginBase::getConfiguration public function Retrieves the plugin's configuration. Overrides SearchApiAutocompleteSuggesterInterface::getConfiguration
SearchApiAutocompleteSuggesterPluginBase::getDescription public function Retrieves the plugin's description. Overrides SearchApiAutocompleteSuggesterInterface::getDescription
SearchApiAutocompleteSuggesterPluginBase::getPluginDefinition public function Retrieves the plugin's definition. Overrides SearchApiAutocompleteSuggesterInterface::getPluginDefinition
SearchApiAutocompleteSuggesterPluginBase::getPluginId public function Retrieves the plugin's ID. Overrides SearchApiAutocompleteSuggesterInterface::getPluginId
SearchApiAutocompleteSuggesterPluginBase::getSearch public function Retrieves the search this plugin is configured for. Overrides SearchApiAutocompleteSuggesterInterface::getSearch
SearchApiAutocompleteSuggesterPluginBase::label public function Retrieves the plugin's label. Overrides SearchApiAutocompleteSuggesterInterface::label
SearchApiAutocompleteSuggesterPluginBase::setConfiguration public function Sets the plugin's configuration. Overrides SearchApiAutocompleteSuggesterInterface::setConfiguration
SearchApiAutocompleteSuggesterPluginBase::validateConfigurationForm public function Validates the plugin's configuration form. Overrides SearchApiAutocompleteSuggesterInterface::validateConfigurationForm
SearchApiAutocompleteSuggesterPluginBase::__construct public function Constructs a SearchApiAutocompleteSuggesterPluginBase.