Undefined array key "boosts" warning in NumberFieldBoost processor on Drupal 11

Created on 3 October 2025, 4 days ago

Problem/Motivation

When enabling and saving the "Number field-based boosting" processor in Drupal 11 (where deprecation warnings are shown by default in development environments), PHP warnings are triggered:

Warning: Undefined array key "boosts" in Drupal\search_api\Plugin\search_api\processor\NumberFieldBoost->submitConfigurationForm() (line 101 of modules/contrib/search_api/src/Plugin/search_api/processor/NumberFieldBoost.php).

Warning: foreach() argument must be of type array|object, null given in Drupal\search_api\Plugin\search_api\processor\NumberFieldBoost->submitConfigurationForm() (line 101 of modules/contrib/search_api/src/Plugin/search_api/processor/NumberFieldBoost.php).

This occurs because the submitConfigurationForm() method attempts to iterate over $values['boosts'] without checking if the key exists first.

When the processor is initially enabled and saved without any field configurations, the boosts key may not be present in the form values.

Steps to reproduce

  1. Install Drupal 11 with the Search API module.
  2. Create a Search API index with a Database or Solr backend.
  3. Navigate to the index’s Processors configuration page.
  4. Enable the "Number field-based boosting" processor.
  5. Save the processor configuration without configuring any boost fields.
  6. Observe PHP warnings in the logs or on-screen (if error display is enabled)

The same issue affects the TypeBoost processor, though it already has partial defensive checks in place.

Proposed resolution

Use the null coalescing operator (?? []) to provide a default empty array when the boosts key is not present.

Before:
foreach ($values['boosts'] as $field_id => $settings) {

After:
foreach ($values['boosts'] ?? [] as $field_id => $settings) {

This defensive programming practice prevents warnings and gracefully handles cases where no boost configurations have been set yet.

Remaining tasks

🐛 Bug report
Status

Active

Version

1.0

Component

General code

Created by

🇪🇸Spain gxleano Cáceres

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

Merge Requests

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