You are here

class SearchApiAutocompleteLiveResultsSuggester in Search API Autocomplete 7

Provides a suggester plugin that retrieves Live Results.

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

Hierarchy

Expanded class hierarchy of SearchApiAutocompleteLiveResultsSuggester

1 string reference to 'SearchApiAutocompleteLiveResultsSuggester'
search_api_autocomplete_search_api_autocomplete_suggester_info in ./search_api_autocomplete.module
Implements hook_search_api_autocomplete_suggester_info().

File

src/SearchApiAutocompleteLiveResultsSuggester.php, line 14
Contains SearchApiAutocompleteLiveResultsSuggester.

View source
class SearchApiAutocompleteLiveResultsSuggester extends SearchApiAutocompleteSuggesterPluginBase {

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

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

    // Add a list of fields to include for autocomplete searches.
    $index = $this
      ->getSearch()
      ->index();
    $fields = $index
      ->getFields();
    $fulltext_fields = $index
      ->getFulltextFields();
    $options = array();
    foreach ($fulltext_fields as $field) {
      $options[$field] = check_plain($fields[$field]['name']);
    }
    $form['display'] = array(
      '#type' => 'radios',
      '#title' => t('Display method'),
      '#description' => t('The way the results should be displayed.'),
      '#options' => array(
        'view_mode' => t("Use view mode: 'Live result search' to display results"),
        'title' => t("Only show title (linked to node)"),
      ),
      '#default_value' => $this->configuration['display'],
      '#access' => $index
        ->getEntityType(),
    );
    $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) {
    try {
      if ($this->configuration['fields']) {
        $query
          ->fields($this->configuration['fields']);
      }
      $query
        ->keys($user_input);
      $results = $query
        ->execute();
    } catch (SearchApiException $e) {
      return array();
    }
    $ret = array();
    $ids = array();
    foreach ((array) $results['results'] as $result) {
      $ids[] = $result['id'];
    }
    $render = NULL;
    if (!empty($ids)) {

      // Load all searched suggested entities.
      $index = $this
        ->getSearch()
        ->index();
      try {
        $datasource = $index
          ->datasource();
      } catch (SearchApiException $e) {
        return $ret;
      }
      $items = $index
        ->loadItems($ids);
      $entity_view = NULL;
      $entity_type = $index
        ->getEntityType();
      if ($entity_type && $this->configuration['display'] == 'view_mode') {
        $entity_view = entity_view($entity_type, $items, 'live_results_search');
      }
      foreach ($items as $id => $item) {
        $url = NULL;
        try {
          if ($entity_view) {
            $render = drupal_render($entity_view[$entity_type][$id]);
          }
          else {
            $render = $datasource
              ->getItemLabel($item);
          }
          $url = $datasource
            ->getItemUrl($item);
        } catch (Exception $e) {
        }
        if (!$url) {
          continue;
        }
        $ret[] = array(
          'url' => url($url['path'], $url['options']),
          'render' => $render,
        );
      }
    }
    return $ret;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAutocompleteLiveResultsSuggester::buildConfigurationForm public function Constructs the plugin's configuration form. Overrides SearchApiAutocompleteSuggesterPluginBase::buildConfigurationForm
SearchApiAutocompleteLiveResultsSuggester::defaultConfiguration public function Retrieves the default configuration for this plugin. Overrides SearchApiAutocompleteSuggesterPluginBase::defaultConfiguration
SearchApiAutocompleteLiveResultsSuggester::getAutocompleteSuggestions public function Retrieves autocompletion suggestions for some user input. Overrides SearchApiAutocompleteSuggesterInterface::getAutocompleteSuggestions
SearchApiAutocompleteLiveResultsSuggester::submitConfigurationForm public function Submits the plugin's configuration form. Overrides SearchApiAutocompleteSuggesterPluginBase::submitConfigurationForm
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::supportsIndex public static function Determines whether this plugin class supports the given index. Overrides SearchApiAutocompleteSuggesterInterface::supportsIndex 1
SearchApiAutocompleteSuggesterPluginBase::validateConfigurationForm public function Validates the plugin's configuration form. Overrides SearchApiAutocompleteSuggesterInterface::validateConfigurationForm
SearchApiAutocompleteSuggesterPluginBase::__construct public function Constructs a SearchApiAutocompleteSuggesterPluginBase.