Problem/Motivation

Date views filter (\Drupal\views\Plugin\views\filter\Date) throw a PHP warning with checked "Allow multiple selections" option.

Warning: Undefined array key "type" in Drupal\views\Plugin\views\filter\Date->acceptExposedInput() (line 128 of core/modules/views/src/Plugin/views/filter/Date.php).

This happens because when the "Allow multiple selections" option is checked the $this->group_info is array.
And this condition in the \Drupal\views\Plugin\views\filter\Date::acceptExposedInput is not enough because $this->group_info['type'] can be an empty and cause a warning:

    if (is_array($this->group_info)) {
        $type = $this->group_info['type'];
    }

Steps to reproduce

  1. Install Drupal standard profile
  2. Edit the default Content view at /admin/structure/views/view/content
  3. Add a "Created" filter
  4. Select "Expose this filter to visitors, to allow them to change it"
  5. Select "Grouped filters"
  6. Set the Grouping 1 label to "Last week", operator to "Is greater than", value type to "An offset from ..." and value "-7 days"
  7. Check "Allow multiple selections"
  8. Press Apply and then Save
  9. Navigate to /admin/content

Proposed resolution

Replace condition in the \Drupal\views\Plugin\views\filter\Date:127
if (is_array($this->group_info)) {
with
if (!empty($this->group_info['type'])) {

Issue fork drupal-3294700

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

dench0 created an issue. See original summary.

dench0’s picture

Issue summary: View changes
dench0’s picture

Issue summary: View changes

dench0’s picture

Status: Active » Needs review
eleonel’s picture

Status: Needs review » Reviewed & tested by the community

I was able to reproduce the issue and your fix solves the problem for me.

eleonel’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new587 bytes

Patch for 10.0.x branch.

lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Nice find.

Something similar was happening in #2811887: Exposed date filter leads to a notice, we should add test coverage here too.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bharath-kondeti made their first commit to this issue’s fork.

bharath-kondeti’s picture

Made few changes on top of the MR. I will update the test cases as well.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

abhinand gokhala k’s picture

StatusFileSize
new3.65 KB

Patch of #11

jodavidson’s picture

After applying the patch of #7 (which fixed the 'type' issue, well done.) I found this error:

Warning: Undefined array key "value" in Drupal\views\Plugin\views\filter\Date->acceptExposedInput() (line 153 of /var/www/html/web/core/modules/views/src/Plugin/views/filter/Date.php)

Looking into the code it seems that $operators[$operator]['values'] can be 1 without $this->value['value'] being set.

Perhaps we can add an isset test to the if statement in this block like:

if (!isset($this->value['value']) || $this->value['value'] == '') {
    return FALSE;
}

I'm suggesting this as a todo for you, as you already have the code and the existing patch available to you for tweaking. If it's preferable for me to try putting the combined patch together I can give it a shot. I hope this isn't too much of an imposition on your time.

phthlaap made their first commit to this issue’s fork.

phthlaap’s picture

This issue happened on my end; this is the view I tested: test_expose_date_filter.yml or please see my screenshot

phthlaap’s picture

phthlaap’s picture

Title: Warning: Undefined array key "type" in Drupal\views\Plugin\views\filter\Date->acceptExposedInput() » Date filter shows error in view exposed group filter with multiple select option.
Status: Needs work » Needs review

I applied a patch and added a test case for this issue.

phthlaap changed the visibility of the branch 3294700-warning-undefined-array-key-type to hidden.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests +Needs Review Queue Initiative

Removing tests tag as coverage appears to be there.

Left some comments on the MR but proposed solution doesn't match the MR

adriancid’s picture

StatusFileSize
new1.5 KB
dieterholvoet’s picture

matthiasm11 made their first commit to this issue’s fork.

littlecoding’s picture

I have another use case that triggers PHP warning here.

Steps to reproduce
Install Drupal standard profile

  1. Edit the default Content view at /admin/structure/views/view/content
  2. Add a "Created" filter
  3. Select "Expose this filter to visitors, to allow them to change it"
  4. Select "Grouped filters"
  5. Set the Grouping 1 label to "Past Month", operator to "Is greater than or equal to", value type to "An offset from ..." and value "-30 days"
  6. No not check "Allow multiple selections"
  7. Press Apply and then Save
  8. Navigate to /admin/content

Note: This may or may not require Search API and an index including the date field used by this filter.

Proposed resolution
Replace condition in the \Drupal\views\Plugin\views\filter\Date:155 (give or take a few lines in dev)
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
with

if (
  !empty($this->options['expose']['use_operator'])
  && !empty($this->options['expose']['operator_id'])
  && isset($input[$this->options['expose']['operator_id']])
) {

Again, this just a PHP warning that has only really started being flagged in PHP 8.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

gugalamaciek made their first commit to this issue’s fork.

baluertl’s picture

Title: Date filter shows error in view exposed group filter with multiple select option. » Date filter shows error in view exposed group filter with multiple select option
Issue summary: View changes

nicrodgers made their first commit to this issue’s fork.

nicrodgers changed the visibility of the branch 3294700-warning-undefined-array-key-type-11.x to hidden.

nicrodgers changed the visibility of the branch 3294700-warning-undefined-array-key-type-11x to hidden.

nicrodgers’s picture

Status: Needs work » Needs review

I've created a new MR that merges in to main, and brought everything up to date. I've hidden the older MRs for clarity.

smustgrave’s picture

Status: Needs review » Needs work
StatusFileSize
new125.45 KB

bug

Verified the bug on the latest main branch, following the steps in the summary.

Applying the MR the error does go away.

Test-only shows the issue (can't run in pipeline)

Behat\Mink\Exception\ElementNotFoundException: Button with id|name|label|value "Apply" not found.
/var/www/html/core/tests/Drupal/Tests/WebAssert.php:159
/var/www/html/core/tests/Drupal/Tests/UiHelperTrait.php:76
/var/www/html/core/modules/datetime/tests/src/Functional/Views/FilterDateTest.php:299

Left a few questions and summary proposed solution doesn't line up perfectly. Can that be cleaned up please

Looks super close.