hook_search_api_solr_documents_alter does not appear to work

Created on 14 June 2017, over 7 years ago
Updated 3 January 2024, 11 months ago

In the api file, this hook is listed:

/**
 * Alter Solr documents before they are sent to Solr for indexing.
 *
 * @param \Solarium\QueryType\Update\Query\Document\Document[] $documents
 *   An array of \Solarium\QueryType\Update\Query\Document\Document objects
 *   ready to be indexed, generated from $items array.
 * @param \Drupal\search_api\IndexInterface $index
 *   The search index for which items are being indexed.
 * @param \Drupal\search_api\Item\ItemInterface[] $items
 *   An array of items to be indexed, keyed by their item IDs.
 */
function hook_search_api_solr_documents_alter(array $documents, \Drupal\search_api\IndexInterface $index, array $items) {
  // Adds a "foo" field with value "bar" to all documents.
  foreach ($documents as $document) {
    $document->setField('foo', 'bar');
  }
}

I attempted to implement it:

/**
 * Alter Solr documents before they are sent to Solr for indexing.
 *
 * @param \Solarium\QueryType\Update\Query\Document\Document[] $documents
 *   An array of \Solarium\QueryType\Update\Query\Document\Document objects
 *   ready to be indexed, generated from $items array.
 * @param \Drupal\search_api\IndexInterface $index
 *   The search index for which items are being indexed.
 * @param \Drupal\search_api\Item\ItemInterface[] $items
 *   An array of items to be indexed, keyed by their item IDs.
 */
function mymodule_search_api_solr_documents_alter(array $documents, IndexInterface $index, array $items) {
  $search_config = \Drupal::config('mymodule.settings');
  $site_id = $search_config->get('site_id');

  // Ensure we have a default value if none set.
  $site_id = !empty($site_id) ? $site_id : 'mymodule';

  // Add the configured site id to all documents.
  foreach ($documents as $document) {
    $document->setField('site_id', $site_id);
  }
}

I set a breakpoint and all of this code is getting hit, the $document object has a new field added. I don't see the custom field in the solr item of the solr admin.

What did I miss?

πŸ› Bug report
Status

Fixed

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States kevinquillen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024