Problem/Motivation

I'm using the custom_value processor on my site. I just saw that if you forgot to set a value on a custom_value field in your index, it throws a warning error :
Warning : Undefined variable $field_value dans Drupal\search_api\Plugin\search_api\processor\CustomValue->addFieldValues() (/home/tmaquin/Bureau/drupalfactory/web/modules/contrib/search_api/src/Plugin/search_api/processor/CustomValue.php ligne 118)
I think this processor shouldn't throw errors even if you misconfigured your custom fields.

Proposed resolution

Could we juste replace in CustomValue the iteration by a empty function ?

if (!empty($field_value)) { $field->addValue($field_value); }
Instead of
if ($field_value !== '') { $field->addValue($field_value); }

Comments

t.maquin created an issue. See original summary.

drunken monkey’s picture

Version: 8.x-1.31 » 8.x-1.x-dev
Category: Task » Bug report
Priority: Minor » Normal
Status: Active » Needs review
StatusFileSize
new1.64 KB
new2.53 KB

Thanks a lot for reporting this problem!

It’s actually even more serious: it seems the problem is not with missing values (those are handled fine), but with values that don’t contain any tokens. Can you confirm that a field like that is present in your index?
(Seems like I was pretty sloppy in reviewing once again, the IDE even displays a warning that the variable might be undefined.)

Anyways, the attached patches both demonstrate the problem (with a regression test) and provide a solution. Please test/review!

t.maquin’s picture

Thanks for your response !
Your patch does the work and yes, i needed to have two custom fields on my index that does not contain any tokens.
I handle theses fields with a custom processor to set specific values in it.

Your patch should be applied to the contrib.

t.maquin’s picture

Status: Needs review » Reviewed & tested by the community

  • drunken monkey committed ac9a698b on 8.x-1.x
    Issue #3406283 by drunken monkey: Fixed "undefined variable" warning in...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Thanks a lot for reporting back, good to hear it worked.
Merged. Thanks again!

Status: Fixed » Closed (fixed)

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