Warning: Undefined array key "value" in Drupal\views\Plugin\views\filter\NumericFilter->acceptExposedInput()

Created on 31 January 2024, 11 months ago
Updated 14 August 2024, 4 months ago

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

  1. Create entity type and entities within that type with two or more entity reference fields.
  2. Create a view filtering on that content type
  3. Add filters for the entity reference fields. For each of those filters:
    1. Select "Expose this filter to visitors..."
    2. Select "Grouped filters"
    3. Select "Allow multiple selections"
    4. (In my case, the operator is "Is not empty (NOT NULL)" and I have one grouping per filter – to get the checkbox)
  4. 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;
  }
}
🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Views 

Last updated about 23 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @goldin
  • Status changed to Needs review 11 months ago
  • 🇮🇳India _utsavsharma

    Created patch addressing the resolution provided.
    Please review.

  • last update 11 months ago
    Custom Commands Failed
  • Status changed to Needs work 11 months ago
  • 🇺🇸United States smustgrave

    Putting 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.

  • 🇺🇸United States greenskin

    We ran into this warning when exposing a filter as type "Grouped filters". Patch resolves the warning for us.

  • 🇺🇸United States generalredneck

    I'm pretty sure this is related to 🐛 Notice: Undefined index: value in Drupal\views\Plugin\views\filter\NumericFilter->acceptExposedInput() Needs work . 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.

  • 🇧🇪Belgium rp7

    Experiencing 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.

  • 🇩🇪Germany FeyP

    Looking into this a bit, the method is called from two places. Once in the submit handler of ViewsExposedForm and then in ViewExecutable::_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 FilterPluginBase will 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 the NumericFilter will 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.

Production build 0.71.5 2024