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().

Issue fork search_api-3549688

Command icon 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

boromino created an issue. See original summary.

boromino’s picture

Status: Active » Needs review
johnny5th’s picture

Thank you! Worked for me!

monph’s picture

Can confirm that the patch worked. Thanks!

mihaic’s picture

The patch worked for us, thanks, can we merge please ? Thanks

drunken monkey made their first commit to this issue’s fork.

drunken monkey’s picture

Component: General code » Plugins
Priority: Normal » Major
Status: Needs review » Fixed

Thanks 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 === NULL over is_null($value)) and then merged.
Thanks again!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

drunken monkey’s picture

Issue tags: +Vienna2025

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.