Problem/Motivation

FilterPluginBase::groupForm() initializes $groups in a conditional that may not always be true and then uses it outside that conditional.

Notice: Undefined variable: groups in Drupal\views\Plugin\views\filter\FilterPluginBase->groupForm() (line 761 of core/modules/views/src/Plugin/views/filter/FilterPluginBase.php).

Steps to reproduce

Install standard profile
Edit the Content view and click on the Content: Published filter
Uncheck the Optional checkbox
Click Remove in both items under the Group options table
Save the filter
You'll get an error on save " Oops, something went wrong. Check your browser's developer console for more details. "
In the logs:
Uncaught PHP Exception TypeError: "count(): Argument #1 ($value) must be of type Countable|array, null given" at /data/app/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php line 1002

Proposed resolution

Initialise $groups before using it.

Remaining tasks

Do it

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-2339921

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

dawehner’s picture

Status: Needs review » Needs work

Let's better define $group = []; by default

almaudoh’s picture

Status: Needs work » Needs review
StatusFileSize
new646 bytes

Done.

    foreach ($this->options['group_info']['group_items'] as $id => $group) {
      if (!empty($group['title'])) {
        $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
      }
    }

I looked further into why my exposed filter was not displayed after I applied this patch and found that I had omitted to define the title when creating the views exposed filter. While that was entirely my fault, it would have saved me a lot of time and effort if the exception thrown was more like 'Your grouped view filter will not be displayed if you don't specify a title'.

jhedstrom’s picture

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

Patch in #2 still applies with fuzz, but should be rerolled for the testbot.

adci_contributor’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new653 bytes

Rerolled. Please review.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for the reroll!

jhedstrom’s picture

Issue summary: View changes

I added a beta phase evaluation to the issue summary.

almaudoh’s picture

Great job, guys! I'm wondering if it would be too much scope creep to add #2

webchick’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs tests

Hm. Sounds like we are missing some test coverage here?

webchick’s picture

Issue tags: +SprintWeekend2015
jhedstrom’s picture

Test coverage for this specific error will be non-trivial, given that from what I can tell, there is currently zero coverage of any of these form methods. (Not that it wouldn't be valuable though.)

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

I filed a follow-up issue #2421023: Create tests for FilterPluginBase form methods to add tests. As I said above, it will be non-trivial, and since this is such an obvious bug fix, I don't think it need be blocked by that task.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll
git ac https://www.drupal.org/files/issues/undefined_variable-2339921-2.patch
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   646  100   646    0     0   2201      0 --:--:-- --:--:-- --:--:--  2219
error: patch failed: core/modules/views/src/Plugin/views/filter/FilterPluginBase.php:749
error: core/modules/views/src/Plugin/views/filter/FilterPluginBase.php: patch does not apply
jhedstrom’s picture

sudheeshps’s picture

Assigned: Unassigned » sudheeshps
Status: Needs work » Needs review
sudheeshps’s picture

Issue tags: +#DCM2015
pwieck’s picture

Issue tags: -Needs reroll

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

As @sudheeshps and @pwieck are wordlessly suggesting, the patch in #4 still applies. So RTBC'ing.

@alexpott: seems you tried committing the patch from #2 iso the rerolled one in #4 :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Given the fact that there are tests see #2421023: Create tests for FilterPluginBase form methods perhaps we can test this.

Anonymous’s picture

Status: Needs review » Needs work
StatusFileSize
new2.79 KB

Would this be a good start for the test?

Anonymous’s picture

Status: Needs work » Needs review
StatusFileSize
new3.31 KB
new95.68 KB
new88.37 KB
new146.33 KB

This patch will fail because of a bug unrelated to this issue: for some reason, after changing the group to "OR" an escaping issue occurs (cf. 2339921_filter_display_bug.png ).

A second issue that came up, is that there is no "Create new filter group" available during the test (cf. other screenshots).

I'd vote to get #4 in, and handle all test coverage in #2421023: Create tests for FilterPluginBase form methods. The issues that come up, could then be handled in child issues and this patch wouldn't be held up.

Status: Needs review » Needs work

The last submitted patch, 21: undefined_variable-2339921-21.patch, failed testing.

Anonymous’s picture

I made a seperate issue for the filter display problem: #2432759: views filter formatting in or group

I added the same test coverage there to demonstrate the issue, so I'm hiding the patch from #20 and #21 for now.

Anonymous’s picture

Related issues:

I found an issue that describes the exact problem I encountered while writing a test.

Anonymous’s picture

Assigned: sudheeshps » Unassigned

The issue in #23 got in, not sure if we need the one from #24 as well, so I'm going to let testbot run over this again.

The last submitted patch, 21: undefined_variable-2339921-21.patch, failed testing.

almaudoh’s picture

+    $rearrange_filter_url = 'http://drupal.xio.local/admin/structure/views/nojs/rearrange-filter/test_filter_plugin_base/default';
+    $this->drupalGet($rearrange_filter_url);

using local domain name here will likely fail on testbot

Anonymous’s picture

Status: Needs work » Needs review
StatusFileSize
new3.29 KB
new792 bytes

Oh yes. Thanks for the pointer!

I looked over the patch again, and this will still not pass since "<" is encoded wrong. I think there is an issue for that somewhere.

Status: Needs review » Needs work

The last submitted patch, 29: undefined_variable-2339921-29.patch, failed testing.

aerozeppelin’s picture

Status: Needs work » Needs review
StatusFileSize
new3.26 KB
new2.29 KB
new3.9 KB

An attempt to reproduce the error and write tests for it

aerozeppelin’s picture

StatusFileSize
new3.35 KB
new5.25 KB

While writing tests for this, i encountered this notice, Notice: Undefined index: #states in Drupal\views\Plugin\views\filter\FilterPluginBase->buildExposedFiltersGroupForm() Here is a fix for it.

The last submitted patch, 31: 2339921-31-test-only-fail.patch, failed testing.

The last submitted patch, 31: 2339921-31.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

guerinteed_mike’s picture

Still seeing issue -> core 8.6.3

dawehner’s picture

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

IMHO we should not

+++ b/core/modules/views_ui/src/Tests/FilterPluginBaseTest.php
@@ -0,0 +1,56 @@
+/**
+ * @file
+ * Contains \Drupal\views_ui\Tests\FilterPluginBaseTest.
+ */
+
+namespace Drupal\views_ui\Tests;
+
+/**
+ * Tests the FilterPluginBase class.
+ *
+ * @group views
+ * @see \Drupal\views\Plugin\views\filter\FilterPluginBase
+ */
+class FilterPluginBaseTest extends UITestBase {

a) Let's remove the @file comment

b) Extend \Drupal\Tests\views_ui\Functional\UITestBase instead and write a browser based test.

We have a test now though.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Searching for duplicates I found #3277134: Count Argument is null and it brings to fatal error when upgrading from PHP 7.4 to PHP 8.1 which is addressing the same problem. I am closing that one in favor of this earlier issue.

+++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
@@ -992,15 +993,17 @@ protected function buildExposedFiltersGroupForm(&$form, FormStateInterface $form
+          if (isset($row['value'][$child]['#states']['visible'])) {

The change starting here is out of scope.

Closing #3277134: Count Argument is null and it brings to fatal error when upgrading from PHP 7.4 to PHP 8.1 as a duplicate.

medha kumari’s picture

Version: 9.4.x-dev » 9.5.x-dev
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new3.26 KB

Rerolled patch #32 in 9.5.x .

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

pcambra’s picture

Status: Needs work » Needs review

I think patch in #50 is removing a bunch of stuff, added a MR bringing #32 up to date, it works for my use case.
Setting to NR to clarify what's left.

pcambra’s picture

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

acbramley’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Figured out how to reproduce this one, updating IS.

acbramley’s picture

Funnily enough fixing this error starts throwing more:

Warning: Undefined array key "status" in Drupal\views\Plugin\views\filter\FilterPluginBase->acceptExposedInput() (line 1619 of core/modules/views/src/Plugin/views/filter/FilterPluginBase.php).
and
Warning: Undefined array key "status" in Drupal\views\Plugin\views\filter\FilterPluginBase->convertExposedInput() (line 1505 of core/modules/views/src/Plugin/views/filter/FilterPluginBase.php).

acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Test coverage appears here https://git.drupalcode.org/issue/drupal-2339921/-/jobs/7228657 I tried to find an existing test that maybe we could expand but this could be expanded on in the future maybe.

Code was seems straight forward and no objections

Going to mark

quietone’s picture

Title: Undefined variable: groups in Drupal\views\Plugin\views\filter\FilterPluginBase->groupForm() » Undefined variable: groups in \Drupal\views\Plugin\views\filter\FilterPluginBase::groupForm
alexpott’s picture

Version: 11.x-dev » 11.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 6419400 and pushed to 11.x. Thanks!
Committed ffcc4c3 and pushed to 11.3.x. Thanks!

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

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

Maintainers, credit people who helped resolve this issue.

  • alexpott committed ffcc4c34 on 11.3.x
    fix: #2339921 Undefined variable: groups in \Drupal\views\Plugin\views\...

  • alexpott committed 64194004 on 11.x
    fix: #2339921 Undefined variable: groups in \Drupal\views\Plugin\views\...
idebr’s picture

This is causing new warnings in tests:

    Time: 00:04.728, Memory: 8.00 MB
    
    Filter Group Form (Drupal\Tests\views\Functional\FilterGroupForm)
     ✘ Filter group form empty
       ┐
       ├ Exception: Deprecated function: Using null as an array offset is deprecated, use an empty string instead
       ├ Drupal\views\Plugin\views\filter\FilterPluginBase->convertExposedInput()() (Line: 1510)

See https://git.drupalcode.org/issue/drupal-3463868/-/pipelines/702131/test_...

  • alexpott committed f6db08c6 on 11.3.x
    Revert "fix: #2339921 Undefined variable: groups in \Drupal\views\Plugin...
alexpott’s picture

Status: Fixed » Needs work

Reverted... we need to fix that.

  • alexpott committed 2c78741c on 11.x
    Revert "fix: #2339921 Undefined variable: groups in \Drupal\views\Plugin...
alexpott’s picture

I rebased the branch MR on top of 11.x so it should fail the same way as HEAD did.

alexpott’s picture

Status: Needs work » Needs review

I've fixed the MR up to not use NULLs as a value for $selected_group.

godotislate’s picture

Status: Needs review » Needs work

MR needs to be rebased against HEAD. Tests are failing because the 11.x commit for #3557585: Update to Composer 2.9.2 is missing.

Also, it might be a good idea for a follow up to document the params and and return type of FilterPluginBase::convertExposedInput().

acbramley’s picture

Status: Needs work » Needs review

Rebase came back green

godotislate’s picture

Status: Needs review » Reviewed & tested by the community

lgtm

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Second time lucky...

Committed and pushed b2c0b060da2 to 11.x and f1f4db7022c to 11.3.x. Thanks!

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

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

Maintainers, credit people who helped resolve this issue.

  • alexpott committed f1f4db70 on 11.3.x
    fix: #2339921 Undefined variable: groups in \Drupal\views\Plugin\views\...

  • alexpott committed b2c0b060 on 11.x
    fix: #2339921 Undefined variable: groups in \Drupal\views\Plugin\views\...

Status: Fixed » Closed (fixed)

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