You are here

public function SearchApiQuery::init in Search API 8

Initialize the plugin.

Parameters

\Drupal\views\ViewExecutable $view: The view object.

\Drupal\views\Plugin\views\display\DisplayPluginBase $display: The display handler.

array $options: The options configured for this plugin.

Overrides PluginBase::init

File

src/Plugin/views/query/SearchApiQuery.php, line 252

Class

SearchApiQuery
Defines a Views query class for searching on Search API indexes.

Namespace

Drupal\search_api\Plugin\views\query

Code

public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  try {
    parent::init($view, $display, $options);
    $this->index = static::getIndexFromTable($view->storage
      ->get('base_table'));
    if (!$this->index) {
      $this
        ->abort(new FormattableMarkup('View %view is not based on Search API but tries to use its query plugin.', [
        '%view' => $view->storage
          ->label(),
      ]));
    }
    $this->query = $this->index
      ->query();
    $this->query
      ->addTag('views');
    $this->query
      ->addTag('views_' . $view
      ->id());
    $display_type = $display
      ->getPluginId();
    if ($display_type === 'rest_export') {
      $display_type = 'rest';
    }
    $this->query
      ->setSearchId("views_{$display_type}:" . $view
      ->id() . '__' . $display->display['id']);
    $this->query
      ->setOption('search_api_view', $view);
  } catch (\Exception $e) {
    $this
      ->abort($e
      ->getMessage());
  }
}