The search page shows a warning with deprecated function
Deprecated function: preg_split(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\search_api\Plugin\search_api\processor\Highlight->highlightField() (line 659 of modules/contrib/search_api/src/Plugin/search_api/processor/Highlight.php).
Steps to reproduce
Search API + Solr + Metatag
I have configured the search view to look into multiple fields (content + keywords & description metatag fields). both metatag fields are set as a fulltext search index.
Searching for a word in the keywords field of an item that has an empty description metatag throws the above warning.
Proposed resolution
The highlightFields method in \Plugin\search_api\processor\Highlight.php should do a check on the field value before calling the highlightField method on the field value.
Remaining tasks
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 3323594-13--fix_bc_break_v2.patch | 2.04 KB | drunken monkey |
Comments
Comment #2
bogdan.dinu commentedI've created a patch with the check proposed in the issue description.
Comment #3
bogdan.dinu commentedComment #4
rpayanmComment #5
drunken monkeyMakes sense, thanks for posting!
Committed.
Comment #7
mkalkbrennerSince the commit of this patch, the search result highlighting integration tests of search_api_solr are failing.
Comment #8
mkalkbrennerUndefined array key "manu"
in
/home/runner/work/search_api_solr/search_api_solr/drupal/modules/contrib/search_api_solr/tests/src/Kernel/SearchApiSolrTechproductsTest.php:54
Comment #9
mkalkbrennerI just verified that reverting the patch lets the test pass again and "Technology" is highlighted again.
Comment #10
mkalkbrennerIn case of "foreign" indexes like Search API Solr Document datasources or Drupal multisite searches, the value could be NULL. In this case the backend is responsible to modify the highlighted_fields extra data later if it allows to use the highlight processor.
Therefore, we must not remove the information about the field that "should" be highlighted at this point. Converting the value into an empty string is the backward compatible behaviour of this processor.
Comment #12
drunken monkeyThanks for noticing and posting a patch, and sorry for breaking your stuff again. ;)
Patch looks good – committed.
Thanks again!
Comment #13
drunken monkeyWhelp, spoke too soon. Problem is actually that
$valuecan be an object of type\Drupal\search_api\Plugin\search_api\data_type\value\TextValueat this point. Whilepreg_split()doesn’t seem to have any problems accepting that as it’s second parameter (for some reason), it of course fails theis_string()check, letting its value be ignored.Instead of all the above, let’s just explicitly cast
$textto a string inhighlightField(), which should also handleNULLvalues correctly.Patch attached (plus interdiff compared to version before any of the patches here was committed), please test/review!
Comment #15
drunken monkeySeems to work, so: committed.
Comment #16
ghalusa commented- Installing drupal/search_api (1.28.0): Extracting archive
- Applying patches for drupal/search_api
https://www.drupal.org/files/issues/2023-02-11/3323594-13--fix_bc_break_... (3323594: PHP 8.1 deprecation warning preg_split)
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2023-02-11/3323594-13--fix_bc_break_...
EDIT: I apologize. I just noticed that this patch is intended for the 8.x-1.x-dev branch.