You are here

function apachesolr_multilingual_apachesolr_modify_query in Apache Solr Multilingual 6

Same name and namespace in other branches
  1. 6.2 apachesolr_multilingual.module \apachesolr_multilingual_apachesolr_modify_query()

Implements hook_apachesolr_modify_query().

File

./apachesolr_multilingual.module, line 332
Multilingual search using Apache Solr.

Code

function apachesolr_multilingual_apachesolr_modify_query(&$query, &$params, $caller) {
  global $language;
  $filter_language = '';

  // We assume that a user is not able to select two different languages as filter
  $language_filters = $query
    ->get_filters('language');
  if (!empty($language_filters)) {
    $filter_language = $language_filters[0]['#value'];
  }
  $languages = variable_get('apachesolr_multilingual_languages', array());
  if (!$filter_language && variable_get('apachesolr_multilingual_auto_language_filter', 0) && (!variable_get('apachesolr_multilingual_auto_language_filter_detachable', 0) || variable_get('apachesolr_multilingual_auto_language_filter_detachable', 0) && empty($_GET['detach-auto-language-filter']))) {
    if (!empty($language->language)) {
      if (in_array($language->language, $languages)) {
        $filter_language = $language->language;
        $query
          ->add_filter('language', $filter_language);
      }
    }
    elseif (variable_get('apachesolr_multilingual_map_language_neutral', '')) {
      $filter_language = variable_get('apachesolr_multilingual_map_language_neutral', '');
      if ($filter_language) {
        $query
          ->add_filter('language', $filter_language);
      }
    }
  }
  if (empty($params['hl.fl'])) {
    $params['hl.fl'] = 'body';
  }
  if ($filter_language && in_array($filter_language, $languages)) {
    $solr = apachesolr_get_solr();
    $fields = $solr
      ->getFields();
    $highlighted_fields = explode(',', $params['hl.fl']);
    foreach ($highlighted_fields as $key => $highlighted_field) {
      if (array_key_exists($highlighted_field . '_' . $filter_language, $fields)) {

        // title, body, spell
        $highlighted_fields[$key] .= '_' . $filter_language;
      }
      elseif (strpos('ts_', $highlighted_field) === 0 && strpos('ts_' . $filter_language, $highlighted_field) !== 0) {
        $highlighted_fields[$key] = 'ts_' . $filter_language . substr($highlighted_field, 2);
      }
      elseif (strpos('tm_', $highlighted_field) === 0 && strpos('tm_' . $filter_language, $highlighted_field) !== 0) {
        $highlighted_fields[$key] = 'tm_' . $filter_language . substr($highlighted_field, 2);
      }
    }
    $params['hl.fl'] = implode(',', $highlighted_fields);
    if (variable_get('apachesolr_search_spellcheck', FALSE) && array_key_exists('spell_' . $filter_language, $fields)) {
      $params['spellcheck.dictionary'] = 'spellchecker_' . $filter_language;
    }
  }
}