Problem/Motivation
If a field to index of a search result is empty, an error occurs because Highlight::prepareTextForExcerpt() expects a string as parameter:
TypeError: Drupal\search_api\Plugin\search_api\processor\Highlight::prepareTextForExcerpt(): Argument #1 ($text) must be of type string, null given, called in /var/www/html/web/modules/contrib/search_api/src/Plugin/search_api/processor/Highlight.php on line 898 in Drupal\search_api\Plugin\search_api\processor\Highlight->prepareTextForExcerpt() (line 678 of modules/contrib/search_api/src/Plugin/search_api/processor/Highlight.php).
Drupal\search_api\Plugin\search_api\processor\Highlight->createFallbackExcerpt(Array, 256) (Line: 550)
Drupal\search_api\Plugin\search_api\processor\Highlight->createExcerptForFields(Array, Array) (Line: 321)
Drupal\search_api\Plugin\search_api\processor\Highlight->addExcerpts(Array, Array, Array) (Line: 273)
Drupal\search_api\Plugin\search_api\processor\Highlight->postprocessSearchResults(Object) (Line: 771)
Drupal\search_api\Entity\Index->postprocessSearchResults(Object) (Line: 620)
Drupal\search_api\Query\Query->postExecute() (Line: 552)
Drupal\search_api\Query\Query->execute() (Line: 622)
Drupal\search_api\Plugin\views\query\SearchApiQuery->execute(Object) (Line: 1513)
Steps to reproduce
- Create a taxonomy term with empty description
- Set the corresponding vocabulary as datasource
- Add the description to the fields to index
- Search for the taxonomy term title
Proposed resolution
Check if the field value is a string before calling Highlight::prepareTextForExcerpt().
Comments
Comment #3
boromino commentedComment #4
johnny5th commentedThank you! Worked for me!
Comment #5
monph commentedCan confirm that the patch worked. Thanks!
Comment #6
mihaic commentedThe patch worked for us, thanks, can we merge please ? Thanks
Comment #8
drunken monkeyThanks a lot for reporting this problem and providing a fix!
Seems quite a few people are running into this, so sorry it took me a bit to get back to you.
Anyways, just made a tiny change regarding code style (I prefer
$value === NULLoveris_null($value)) and then merged.Thanks again!
Comment #11
drunken monkey