You are here

function search_api_federated_solr_search_api_solr_documents_alter in Search API Federated Solr 8.3

Same name and namespace in other branches
  1. 4.x search_api_federated_solr.module \search_api_federated_solr_search_api_solr_documents_alter()

Alter Solr documents before they are sent to Solr for indexing.

Parameters

\Solarium\QueryType\Update\Query\Document[] $documents: An array of \Solarium\QueryType\Update\Query\Document\Document objects ready to be indexed, generated from $items array.

\Drupal\search_api\IndexInterface $index: The search index for which items are being indexed.

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be indexed, keyed by their item IDs.

File

./search_api_federated_solr.module, line 161
Contains hook implementations for the Federated Solr Search API Module.

Code

function search_api_federated_solr_search_api_solr_documents_alter(array &$documents, \Drupal\search_api\IndexInterface $index, array $items) {

  // The 8.x-3.x version of Search API Solr behaves differently from 7.x-1.x.
  // Notably, it uses Hungarian notation for fulltext fields, such that
  // "tm_rendered_item" is indexed as "tm_X3b_en_rendered_item". We rely on consistent
  // field naming, so have to coerce that field and the "content" field, which
  // are used by the schema from Drupal 7.
  foreach ($documents as $document) {
    $fields = $document
      ->getFields();
    foreach ($fields as $field_id => $field) {

      // The Drupal 8 schema passes 'sort_', so ignore that. We only index the
      // default language, and Drupal 8 is language-sensitive. So we find items
      // that are named 'tm_*_LANGCODE_rendered_item' and use those.
      $default_language_id = \Drupal::languageManager()
        ->getDefaultLanguage()
        ->getId();
      $string = $default_language_id . '_rendered_item';
      if (substr_count($field_id, $string) > 0 && substr_count($field_id, 'sort_') === 0) {
        $boost = $document
          ->getFieldBoost($field_id);
        $modifier = $document
          ->getFieldModifier($field_id);

        // The 'content' field is the generic handler for the Drupal 7 schema version.
        if (is_array($field)) {
          $string = implode(" ", $field);
          if (!is_null($string)) {
            $document
              ->setField('content', $string, $boost, $modifier);
          }
        }

        // The 'tm_rendered_item' field is required by the application.
        $document
          ->setField('tm_rendered_item', $field, $boost, $modifier);
      }
    }
  }
}