- Merge request !283Undefined array key boosts warning in NumberFieldBoost processor on Drupal 11 → (Open) created by Unnamed author
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.
The same issue affects the TypeBoost processor, though it already has partial defensive checks in place.
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.
Active
1.0
General code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.