Problem/Motivation
When exposed, the date filters 'empty' and one 'not empty' don't work.
Proposed resolution
Fix the logic in Drupal\views\Plugin\views\filter\Date::acceptExposedInput() to account for how 'empty' and 'not empty' work.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Original Reoport
1. Add a date field to a new content type.
2. Create two new items of that content type, one with the date field populated and one with it empty.
3. Create a new view with a filter on the date field, select the "is not empty (NOT NULL)" filter.
Notice at the moment the view output is correctly filtered so that only the content with a date is shown. Now...
4. Expose the date filter. Select grouped. Add 2 options: one "empty" and one "not empty".
Back to the view output and changing the filter to "empty" or "not empty" makes no difference to the result, you'd expect it to be filtered on the presence of a value in the date field (as per the result of step 3) but instead all items are always shown.
During this experimentation I encountered https://www.drupal.org/node/2369119, the patch https://www.drupal.org/files/issues/2369119-196.patch fixed the error while saving the view but still the above issue is not resolved. This is potentially a dupe of or related to one of the related or referenced issues of it but I did not see my exact situation so was not sure where to post.
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | 2865344-44.patch | 7.48 KB | alexpott |
| #44 | 42-44-interdiff.txt | 1.72 KB | alexpott |
| #42 | interdiff-2865344-41-42.txt | 1004 bytes | mbovan |
| #42 | 2865344-42.patch | 7.89 KB | mbovan |
| #41 | 2865344-41.patch | 7.89 KB | mbovan |
Comments
Comment #2
lendudeI can reproduce this with a Date field but not a Timestamp field, so moving this to the Datetime module for now.
Also different is that when using the Date filter the 'value' column for the grouped filter isn't cleared when you select 'is empty', that does happen for the timestamp field. No idea if this is related.
Comment #3
mpdonadioDowngrading this, not sure if it truly classifies as a major.
Think we need a test-only patch to demonstrate the problem (or is someone wants to attach an exported view).
Wondering if this is related to that Notice issue in views+datetime that I can't locate right now.
Comment #4
lendudeHere is a test for this. Also added a test to show that it works fine if you don't expose it.
Since just exposing it when using the 'empty' or 'not empty' operator and not using a group makes very little practical sense, I don't think we need a test for that (I wouldn't even know what you would expect to see).
No idea what the fix should be at the moment.
Comment #7
organicwire commentedConfirming the problem for Drupal 8.3.7 and 8.4.0-rc1.
Comment #8
organicwire commentedThis patch fixes the problem.
Comment #10
organicwire commentedComment #11
organicwire commentedComment #12
mpdonadioExtreme nit, but `elseif` is the Drupal coding standard.
This should be merged with the test in #4, but this does looks correct per the similar logic in Date::validateValidTime(), and NumericFilter::acceptExposedInput().
NW for the test.
Comment #13
organicwire commentedMerged with #4 and changed to elseif.
Comment #15
mpdonadio#2865992: Convert Datetime module Views tests to Kerneltest changed FilterDateTest from a functional test to a kernel test, so the patch in #4 needs to live somewhere else. Probably needs to be a new functional (ie, BrowserTestBase) test in the Drupal\Tests\datetime\Functional\Views namespace. Or, the final bit of the test needs to be converted to a kernel test.
Comment #16
organicwire commentedWho can provide a patch for FilterDateTest? It seems to me that I don't fully understand the kernel test architecture. Specifically NodeCreationTrait does not work out of the box because it has filter.module dependencies. Also, I can't use $this->drupalPostForm() anymore.
Comment #17
lendude@organicwire in a kernel test just use NodeType::create() and Node::create() and not the Trait. Using $this->drupalPostForm() would require a browser, which kernel test doesn't have.
So like @mpdonadio said in #15
Create a new functional test for this and move the code I wrote in #4 to the new test is probably the best way to go. We want this to be a functional test, since we really want to test that this is rendered correctly.
Comment #18
OnkelTem commentedSo date filters still don't ok. Fine!
Let's wait another decade until 1000 lines of tests are magically written for a 1-line change.
Comment #19
mpdonadioMoved the kernel bits to the kernel test and cobbled together a rough functional test (needs cleanup). Not readily apparent what I botched.
Comment #21
lendudeAdded the @group and stopped using the preview (which has a problem with exposed filters), and tested a real page. Green locally now.
Comment #22
lendudeOops forgot to upload the interdiff, here we go.
Comment #24
lendudeDuh..
Comment #25
mpdonadioGoing to fix a few nits tonight, and do a proper IS.
Comment #26
mpdonadioPicked some phpcs nits.
After we get an RTBC we will have to do a 8.4.x version b/c #2826404: Create DateTimeItemInterface and deprecate global constants in datetime.module and possibly #2627512: Datetime Views plugins don't support timezones (didn't look closely where the merge conflicts were).
Comment #27
mpdonadioComment #28
lendudeAdding the 8.4.x version here.
Wrote a bit too much of this to feel 100% comfortable RTBC'ing this but I feel this is ready.
Comment #30
mpdonadioThis should fix the 8.4 fail, and here is a re-upload of the 8.5.x version renamed.
Comment #32
didebru#30 worked great thanks :) on 8.4.x
Comment #33
jhedstromJust a nit here.
Could we add a code comment here to explain what's going on? It isn't clear why if the operator has 2 values we care about min and max being empty strings.
Aside from that, this looks great and RTBC.
Comment #34
borisson_Setting this to needs work based on #33.
Comment #35
jibranHow about this?
Comment #37
jhedstromI've re-queued the test, but assuming it comes back green, I think this is RTBC.
Comment #38
larowlanany reason why these are static?
nit, we should wrap these, > 80
Comment #39
mbovan commentedI tested #35, it seems to be working fine for us. 👍
Addressed points from #38.
Comment #40
berdirI still find it strange to have this as a property at all, we only use it during setup, also strange comment, it's not today, it's now.
What I would do is something like this:
$now = \Drupal::time()->getRequestTime();
And then 3x a version of:
DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
Then the variable name and so on is pretty self-explanatory, no property needed and it's shorter.
(the custom argument for format() should IMHO be deprecated/discouraged, just use ->format() directly, what happens internally is that we create a DateTime object again and call format() on it.
Comment #41
mbovan commentedAddressed #40. Thanks for the review @Berdir!
Comment #42
mbovan commentedWe are using this patch with #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter and it looks like it broke our tests with a date range field.
Steps to reproduce:
In the mentioned use-case,
$this->value['value']hasNULLvalue which does not work with strict comparison from before$this->value['value'] === ''.The attached patch should fix described use-case as well as the original problem from the issue summary.
I'm not sure how to write a test since it only happens in combination with #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter...
Comment #43
berdirI don't think there is a need to have those checks type safe. The feedback about the test has been addressed.
Comment #44
alexpottLet's not introduce a new pattern into views filter plugins by using a
switchstatement it poses more questions than it answers for me. Like what should the default action be and what are the other values possible - this thing can equal 0.Also fixed a code style issue in one of the tests.
Comment #45
alexpottCredited @jhedstrom, @Berdir, @larowlan for review comments
Credited @bobemoe for creating the issue.
Committed and pushed 8e3bcf48ee to 8.7.x and 442ddf11f0 to 8.6.x. Thanks!
Comment #49
emb03 commentedI think this change is causing an issue for me. I am using date field in views for my events. When the date field is empty it outputs today's date. I don't want it to output anything.