Problem/Motivation
Views exposed filter on an entity reference field generates a php warning with grouped filter type set to "Allow multiple selections." In my case, the warning happened after adding more than one distinct entity reference field filter.
Warning: Undefined array key "value" in Drupal\views\Plugin\views\filter\NumericFilter->acceptExposedInput() (line 442 of core/modules/views/src/Plugin/views/filter/NumericFilter.php).
I think this is happening because the code is trying to access the 'values' key in array $info[$this->operator] but really should be checking whether the 'values' key exists before attempting to access it.
Steps to reproduce
- Create entity type and entities within that type with two or more entity reference fields.
- Create a view filtering on that content type
- Add filters for the entity reference fields. For each of those filters:
- Select "Expose this filter to visitors..."
- Select "Grouped filters"
- Select "Allow multiple selections"
- (In my case, the operator is "Is not empty (NOT NULL)" and I have one grouping per filter – to get the checkbox)
- Visit the display of the view. The error appears on page load before you select anything.
The filters work, but the warning is persistent.
Proposed resolution
In web/core/modules/views/src/Plugin/views/filter/NumericFilter.php replace this:
$info = $this->operators();
if (!empty($info[$this->operator]['values'])) {
switch ($info[$this->operator]['values']) {
case 1:
if ($value['value'] === '') {
return FALSE;
}
break;
case 2:
if ($value['min'] === '' && $value['max'] === '') {
return FALSE;
}
break;
}
}
with:
$info = $this->operators();
if (isset($info[$this->operator]['values'])) {
switch ($info[$this->operator]['values']) {
case 1:
if (isset($value['value']) && $value['value'] === '') {
return FALSE;
}
break;
case 2:
if (isset($value['min'], $value['max']) && $value['min'] === '' && $value['max'] === '') {
return FALSE;
}
break;
}
}| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3418238-2.patch | 1.12 KB | _utsavsharma |
| #2 | interdiff_2.txt | 1.12 KB | _utsavsharma |
Comments
Comment #2
_utsavsharma commentedCreated patch addressing the resolution provided.
Please review.
Comment #3
smustgrave commentedPutting just an isset() usually isn't the solution, as that will mask a larger issue.
Once more research done as to why this key isn't defined. test coverage will be needed.
Comment #5
greenskin commentedWe ran into this warning when exposing a filter as type "Grouped filters". Patch resolves the warning for us.
Comment #6
generalredneckI'm pretty sure this is related to #2825860: Notice: Undefined index: value in Drupal\views\Plugin\views\filter\NumericFilter->acceptExposedInput(). It may possible be a duplicate? though I landed here because I"m getting a similar issue even after upgrading to 10.2.5 where the prior fix was released.
Comment #7
rp7 commentedExperiencing the same issue on a project of ours. Running on 10.2.6. Patch fixes it, but I agree with #3 that this isn't really a long term solution.
Comment #8
feyp commentedLooking into this a bit, the method is called from two places. Once in the submit handler of
ViewsExposedFormand then inViewExecutable::_build().The first call triggers the warning for this filter configuration, because at that time, the exposed input is not yet converted from the value of the exposed form element to the structure actually expected by the handler. You get an array with the selected option groups, e.g. something like
[1 => 1, 2 => 2, 3 => 3], if you have three option groups and all are selected.The parent method in
FilterPluginBasewill handle this and accept the input, but then the check in the switch statement will trigger the warning, because a) there is no handling for multiple values and b) the values have not yet been converted to the expected structure[min => .., max => .., value => ..].For the subsequent calls from
ViewsExecutable, before calling the handler the input will be properly converted to the expected format and then checked for each selected option group separately, so theNumericFilterwill then accept the input via the switch statement.This might have been designed this way with the idea that the exposed input for grouped filters is initially checked by the parent method only and then the real check only happens in
ViewsExecutable. If this assumption was correct, then checking whether the array keys are set would be the correct fix at least for this filter configuration.If we don't want to go with that, I think we would need to change the submit handler of the exposed form so that it checks the value in a similar way to how it is done in
ViewsExecutable::_build()for this kind of filter configuration, i.e. convert everything to the expected format and then check each value separately.Comment #9
mably commentedBeing hit by the same problem on Drupal 11.1.6.
Our logs are polluted by these warnings.
Patch seems to work fine.
Comment #10
matthiasm11 commentedThe patch here overlaps with #3294700: Date filter shows error in view exposed group filter with multiple select option. I propose to fix this in #3294700: Date filter shows error in view exposed group filter with multiple select option since it has a pull request and also addresses the same issue in the date filter.