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
- Install Drupal 11 with the Search API module.
- Create a Search API index with a Database or Solr backend.
- Navigate to the index’s Processors configuration page.
- Enable the "Number field-based boosting" processor.
- Save the processor configuration without configuring any boost fields.
- 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
Issue fork search_api-3550113
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
ishani patel commented@gxleano, I've been facing the same issue and have resolved it.
Raised MR, kindly check and review.
Thank you!
Comment #5
ishani patel commentedComment #6
gxleano commentedThanks @ishani patel, it is already working as expected
So, moving to RTBC.
Comment #7
gxleano commentedComment #8
drunken monkeyThanks for reporting this problem, @gxleano, and thanks @ishani patel for providing the MR.
Seems simple enough, and it’s clear how this would happen. We can probably also get away with not providing a regression test for this.
So, merged. Thanks again!
Comment #11
drunken monkey