Needs work
Project:
Drupal core
Version:
main
Component:
views.module
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
2 Jul 2018 at 10:11 UTC
Updated:
8 Sep 2025 at 07:11 UTC
Jump to comment: Most recent, Most recent file



Comments
Comment #2
panagiotisp commentedIn my opinion a flexible solution is to add another checkbox in order to allow the administrator to decide what to show: the computed dates or the offsets.
Comment #3
q__nt_n#2 I'm agree with you but in which case do we want a relative text string ?
Comment #4
q__nt_nComment #5
q__nt_nI think, we must have a datepicker in filter date field (cf screenshot).

What do you think?
Comment #6
john cook commented@q__nt_n, I was using a contrib date picker which was why I found the problem. A date picker won't work unless the date is in the correct format.
There's two issues:
I think first we should fix the date format. This would allow contrib/custom to change the fields to be date picker.
A new feature request can be made to change the existing form element to be a date picker.
Comment #8
vensiresI also agree we should fix the date format. Right now the user has the screen showing words like "today" and "tomorrow" in my case instead of the expected dates.
John Cook, jQuery UI's datepicker works correctly even in this case though. Other datepickers (ex. Pikaday - if I recognize correctly from your screenshot) may fail though.
Comment #12
graber commentedHere's a patch that changes behaviour of the date filter however by converting relative date values to dates on init.
I think it simplifies the date filter a lot and makes it usable with datepickers being the standard everywhere.
Also fixed coding standards issues in the file and added support for an empty min or max value for between filter.
I expect some core tests to fail unfortunately as this is a functional change. Here goes.
Comment #14
graber commentedOne more try..
Comment #15
graber commentedComment #17
graber commentedAfter making sure FilterDateTest passes locally..
Comment #19
graber commentedOverriding the query() method was not an option as datetime and datetime_range filters extend this class.
Comment #20
graber commentedNice. Including a patch for 8.9.x as well.
Comment #21
snehalgaikwad commentedTested patch #19 on 9.1.x and applied cleanly. Working correct for date filter with offset in views. Attaching screenshots below for reference.
Comment #22
bucefal91 commentedI've spent some time poking this patch locally on a dummy Drupal 8.9 (i.e. patch #20) and wow, Marcin, that's pretty cool stuff :) I could do dates like
$field BETWEEN '-10 days' AND '2021-01-01'.I've tried a set of different operators, values, exposed/not-exposed. Everything has been a smooth flight except the grouped exposed filter. For some reason (I did not attempt to explore it on the code level), it would not accept any value - no matter if it's a YYYY-MM-DD or a relative string. (Attaching a screenshot where I captured the issue).
In general, I personally consider it a step forward for this filter :) It seems the code has become slightly simpler because we no longer maintain such explicit distinction between 'relative' vs 'absolute' date, which I deem a good sign :)
(based on the issue with the grouped filter, I am bumping the status into "needs work").
Comment #23
graber commentedThanks for your review Alex, there is a small thing with input validation ("between" and "not between" could accept one or 2 values), still not sure though how validation failed in your case where you provided exactly the amount.. What I'm getting for your set of grouped filters is: (attached screenshot).. If I'll fill the last label, validation passes.
Can you provide your view config?
Comment #25
juanolalla commentedI've tried #20 with a single date filter with the operator "is greater than or equal to". When using offset values like "+1 day" it appears in the form as the same offset value, not formatted into the corresponding date.
I've debugged this and I saw that it's using an array
$componentwith the values['min', 'max', 'value'], but with this filter there is no such thing as$form['value']['value']['#default_value'], but$form['value']['#default_value']instead.I tried adjusting the code to format
$form['value']['#default_value'], but despite the default value was correctly formatted, it doesn't appear in the form. Setting$form['value']['#value'] = $form['value']['#default_value']would do that. I'm not sure that would be the right thing to do though.Comment #26
douggreen commentedThe patch no longer applies (even to 9.1.x) and needs a re-roll for 9.2.x.
IMO we should just fix the problem and leave the code style fixes for another patch. I'm all for fixing code style issues in core (I wrote the original coder module), but this is more likely to get committed if we just keep the changes to the fixes, and leave the code style comments for another issue.
A few problems / questions:
preg_match()regex won't catch day offsets like+1 day, we need something better here. I couldn't find a PHP function that matches all of the valid relative date format. So we might just trystrpbrk($form['value'][$component]['#default_value'], '/.,') !== FALSEWe don't need to doisset()checks withempty(), asempty()covers this.Comment #27
douggreen commentedHere's a straight re-roll for 9.1.x and 9.2.x.
Comment #28
douggreen commentedAnd this has the problems that @juanolalla mentions above. It's altering the wrong elements (at least on 9.x).
Comment #29
nishat ahmad commentedComment #30
nishat ahmad commentedComment #31
anmolgoyal74 commentedRe-rolled for 9.2.x
Comment #33
rwohlebTrying to apply the patch from #31 on D9.2.6 failed in the portion adjusting the tests. For some reason the order of parameters passed in the assertion swapped. Here is an adjusted version.
Comment #35
khiminrm commentedUpdated the patch to last Drupal 9.4.x
Comment #38
benstallings commentedBuilding on #34 to add a day to the max date if it does not specify an hour. See https://www.drupal.org/project/drupal/issues/2842409 for another approach that is incompatible with the approach taken by #34.
Comment #39
sonam.chaturvedi commentedVerified and tested patch#38. Patch applied successfully on 9.5.x-dev.
Test Steps:
1. Goto /admin/structure/views/view/content
2. Click 'Add' for 'Filter criteria' > Select date field
3. Check 'Expose this filter to visitors, to allow them to change it' option
4. Select 'in between' operator and option 'offset' for date
5. Goto Content overview page > verify date/time is not displayed as relative string, should be displayed in date format.
Test Result:
When using a date/time filed as exposed filter using the "offset from the current time" setting, the filter is displayed in date format, and not relative string
Before Patch:

After Patch:

RTBC +1
Comment #40
benstallings commentedThank you, @sonam.chaturvedi !
Comment #42
mradcliffeRemoving this means that all view config entities that use this setting need to be updated in a post-update function.
We also don't have a test that asserts the bug.
After I apply this patch I was confused because I lost the date/offset option, and that isn't explained in the proposed resolution so I'm adding the "Needs issue summary update" tag.
Comment #44
yes_max commentedWarning: Undefined array key "max" in Drupal\views\Plugin\views\filter\Date->opBetween() (line 198 of /app/web/core/modules/views/src/Plugin/views/filter/Date.php)I'm getting this warning associated with the patch, which then leads to this database exception and a WSOD:
Drupal\Core\Database\DatabaseExceptionWrapper: ExceptionI applied my changes to #38 to fix the warning.
Comment #45
graber commentedI finally created this https://www.drupal.org/project/date_filter to address this issue and many other.
Looks like issues like this have a very small chance to get enough attention and find their way into core so we have to handle that in contrib :(
Comment #46
junaidpvBuilding on #44. Adding similar improvement as #38 but to the datetime filter in core datetime module.
Comment #47
junaidpvTry fixing CCF.
Comment #48
smustgrave commentedWas previously tagged for tests and issue summary update which still need to happen plese.
Also please include interdiffs with patches.
Comment #50
junaidpvExactly same as #47, just re-rolled for 10.1.x
Comment #51
s3b0un3tHello,
I tried to apply patch on comment #50, but it's thrown an error in datetime module :
Without this warning, the patch works perfectly on Drupal 10.2.2!
Comment #52
prudloff commentedStarting with #38, the patch always adds
+1 dayto any max value that does not explicitly contain a time.While I understand the reason to do this on date values, I don't think it always makes sense on offset values.
For example "now" will become "now +1 day" (so tomorrow at the same time). But if I want my view to return results between some date and now, it will be confusing if it starts returning results from tomorrow.
Comment #53
junaidpvJust a re-roll of #50 for 10.3.2
Hopefully, we need to address #51 and #52.
Comment #54
socialnicheguru commentedconflicts with #2842409: Date filter selecting the same day in between filter returns no results
Comment #55
plopescAdded new version of the patch against 10.3.2 where it adds support also for missing max value.
Comment #56
sunnygambino commentedPatch based on 2982968-core-views-improve-date-filter-56.patch.
Just quick fix to make sure there are no warnings, otherwise AJAX reset button won't work, as it is throwing these annoying warning. :)
Comment #57
johnvComment #58
johnvThe OP is confusing to me on several accounts
- the expected outcome is in +/- 3 days, but the actual results are in years
- the printscreen shows that 'An offset [...]' is selected, instead of 'A date [...]'.
I my test, when selecting
- 'An offset [...]', I need to set '-7 days', to '+1 day'
- 'A date [...]', I need to set '20241231' to '20250303'
IMO that is OK. I can imagine a view with changes in the last week (using offset).
Of course the widget is very basic/not acceptable for ordinary users.
So, this issue still 'Needs issue summary update'
Comment #59
niranjan_panem commentedBelow is the patch, in drupal 11
Comment #60
s3b0un3tHello @niranjan_panem,
You're patch for Drupal 11 is incorrect. There are comments conflict into your patch:
Comment #62
ressaIssue comment patches have been phased out, and the work flow now is to create an issue fork, and then add a GitLab MR instead. I created a branch "2982968-date-exposed-filter" for this.