You are here

protected function SearchApiQuery::addLanguages in Search API 7

Adds language filters for the query.

Internal helper function.

Parameters

array $languages: The languages for which results should be returned.

Throws

SearchApiException If there was a logical error in the combination of filters and languages.

1 call to SearchApiQuery::addLanguages()
SearchApiQuery::preExecute in includes/query.inc
Prepares the query object for the search.

File

includes/query.inc, line 669
Contains SearchApiQueryInterface and SearchApiQuery.

Class

SearchApiQuery
Provides a standard implementation of the SearchApiQueryInterface.

Code

protected function addLanguages(array $languages) {
  if (array_search(LANGUAGE_NONE, $languages) === FALSE) {
    $languages[] = LANGUAGE_NONE;
  }
  $languages = drupal_map_assoc($languages);
  $langs_to_add = $languages;
  $filters = $this->filter
    ->getFilters();
  while ($filters && $langs_to_add) {
    $filter = array_shift($filters);
    if (is_array($filter)) {
      if ($filter[0] == 'search_api_language' && $filter[2] == '=') {
        $lang = $filter[1];
        if (isset($languages[$lang])) {
          unset($langs_to_add[$lang]);
        }
        else {
          throw new SearchApiException(t('Impossible combination of filters and languages. There is a filter for "@language", but allowed languages are: "@languages".', array(
            '@language' => $lang,
            '@languages' => implode('", "', $languages),
          )));
        }
      }
    }
    else {
      if ($filter
        ->getConjunction() == 'AND') {
        $filters += $filter
          ->getFilters();
      }
    }
  }
  if ($langs_to_add) {
    if (count($langs_to_add) == 1) {
      $this
        ->condition('search_api_language', reset($langs_to_add));
    }
    else {
      $filter = $this
        ->createFilter('OR');
      foreach ($langs_to_add as $lang) {
        $filter
          ->condition('search_api_language', $lang);
      }
      $this
        ->filter($filter);
    }
  }
}