You are here

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

@inheritdoc

File

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

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 alterSolrResponseBody(&$body, QueryInterface $query) {
  $data = json_decode($body);
  $index = $query
    ->getIndex();
  $field_names = $this
    ->getSolrFieldNames($index, TRUE);
  $doc_languages = [];
  if (isset($data->response)) {
    foreach ($data->response->docs as $doc) {
      $language_id = $doc_languages[$this
        ->createId($index
        ->id(), $doc->{SEARCH_API_ID_FIELD_NAME})] = $doc->{$field_names[SEARCH_API_LANGUAGE_FIELD_NAME]};
      foreach (array_keys(get_object_vars($doc)) as $language_specific_field_name) {
        $field_name = Utility::getSolrDynamicFieldNameForLanguageSpecificSolrDynamicFieldName($language_specific_field_name);
        if ($field_name != $language_specific_field_name) {
          if (Utility::getLanguageIdFromLanguageSpecificSolrDynamicFieldName($language_specific_field_name) == $language_id) {
            $doc->{$field_name} = $doc->{$language_specific_field_name};
            unset($doc->{$language_specific_field_name});
          }
        }
      }
    }
  }
  if (isset($data->highlighting)) {
    foreach ($data->highlighting as $solr_id => &$item) {
      foreach (array_keys(get_object_vars($item)) as $language_specific_field_name) {
        $field_name = Utility::getSolrDynamicFieldNameForLanguageSpecificSolrDynamicFieldName($language_specific_field_name);
        if ($field_name != $language_specific_field_name) {
          if (Utility::getLanguageIdFromLanguageSpecificSolrDynamicFieldName($language_specific_field_name) == $doc_languages[$solr_id]) {
            $item->{$field_name} = $item->{$language_specific_field_name};
            unset($item->{$language_specific_field_name});
          }
        }
      }
    }
  }
  if (isset($data->facet_counts)) {
    $facet_set_helper = new FacetSet();
    foreach (get_object_vars($data->facet_counts->facet_fields) as $language_specific_field_name => $facet_terms) {
      $field_name = Utility::getSolrDynamicFieldNameForLanguageSpecificSolrDynamicFieldName($language_specific_field_name);
      if ($field_name != $language_specific_field_name) {
        if (isset($data->facet_counts->facet_fields->{$field_name})) {

          // @todo this simple merge of all language specific fields to one
          //   language unspecific fields should be configurable.
          $key_value = $facet_set_helper
            ->convertToKeyValueArray($data->facet_counts->facet_fields->{$field_name}) + $facet_set_helper
            ->convertToKeyValueArray($facet_terms);
          $facet_terms = [];
          foreach ($key_value as $key => $value) {

            // @todo check for NULL key of "missing facets".
            $facet_terms[] = $key;
            $facet_terms[] = $value;
          }
        }
        $data->facet_counts->facet_fields->{$field_name} = $facet_terms;
      }
    }
  }
  $body = json_encode($data);
}