You are here

protected function AbstractSearchApiSolrMultilingualBackend::alterSearchApiQuery in Search API Multilingual Solr Search 8

Adjusts the language filter before converting the query into a Solr query.

Parameters

\Drupal\search_api\Query\QueryInterface $query: The \Drupal\search_api\Query\Query object.

File

src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php, line 135

Class

AbstractSearchApiSolrMultilingualBackend
A abstract base class for all multilingual Solr Search API backends.

Namespace

Drupal\search_api_solr_multilingual\Plugin\search_api\backend

Code

protected function alterSearchApiQuery(QueryInterface $query) {

  // Do not modify 'Server index status' queries.
  // @see https://www.drupal.org/node/2668852
  if ($query
    ->hasTag('server_index_status') || $query
    ->hasTag('mlt')) {
    return;
  }
  parent::alterSearchApiQuery($query);
  $languages = $query
    ->getLanguages();

  // If there are no languages set, we need to set them.
  // As an example, a language might be set by a filter in a search view.
  if (empty($languages)) {
    if (!$query
      ->hasTag('views') && $this->configuration['sasm_limit_search_page_to_content_language']) {

      // Limit the language to the current language being used.
      $languages[] = \Drupal::languageManager()
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
    }
    else {

      // If the query is generated by views and/or the query isn't limited by
      // any languages we have to search for all languages using their
      // specific fields.
      $languages = array_keys(\Drupal::languageManager()
        ->getLanguages());
    }
  }
  if ($this->configuration['sasm_search_page_include_language_independent']) {
    $languages[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    $languages[] = LanguageInterface::LANGCODE_NOT_APPLICABLE;
  }
  $query
    ->setLanguages($languages);
}