Problem/Motivation

When using a date/time filed as exposed filter using the "offset from the current time" setting, the filter doesn't display as I expected.

The setting for the filter are:

This leads to the filter being displayed like:

As you can see the dates are output in the filter as the relative text string. This can lead to problems if using a date selector dialog.

Proposed resolution

The output I expected is:

This has the dates computed and entered in the input element.

CommentFileSizeAuthor
#59 timestamp-added-to-offset.patch2.52 KBniranjan_panem
#56 2982968-core-views-improve-date-filter-57.patch10.04 KBsunnygambino
#55 interdiff-53-56.txt1.59 KBplopesc
#55 2982968-core-views-improve-date-filter-56.patch10.05 KBplopesc
#53 2982968-core-views-improve-date-filter-53.patch9.02 KBjunaidpv
#50 2982968-core-views-improve-date-filter-49.patch9 KBjunaidpv
#47 2982968-core-views-improve-date-filter-47.patch8.99 KBjunaidpv
#46 2982968-core-views-improve-date-filter-46.patch9 KBjunaidpv
#44 2982968-core-views-improve-date-filter-44.patch8.19 KByes_max
#39 after_pat38.png62.65 KBsonam.chaturvedi
#39 before_pat38.png61.6 KBsonam.chaturvedi
#38 2982968-core-views-improve-date-filter-38.patch8.12 KBbenstallings
#35 2982968-core-views-improve-date-filter-34.patch11.59 KBkhiminrm
#33 2982968-core-views-improve-date-filter-33.patch11.7 KBrwohleb
#31 2982968-core-views-improve-date-filter-31.patch11.7 KBanmolgoyal74
#30 2982968-core-views-improve-date-filter-29.patch11.97 KBnishat ahmad
#29 2982968-core-views-improve-date-filter-29.patch.txt11.97 KBnishat ahmad
#27 2982968-27-views-relative-date-exposed-filter.patch11.1 KBdouggreen
#23 Screenshot 2020-10-08 at 16.32.23.png253.09 KBgraber
#22 Screenshot from 2020-10-07 18-46-57.png211.92 KBbucefal91
#21 exposed-filter.png53.88 KBsnehalgaikwad
#21 configure-filter.png140.12 KBsnehalgaikwad
#20 2982968-core-views-improve-date-filter-8.9.x-20.patch12.02 KBgraber
#19 2982968-core-views-improve-date-filter-19.patch11.97 KBgraber
#17 2982968-core-views-improve-date-filter-17.patch11.93 KBgraber
#15 2982968-core-views-simplify-date-filter-15.patch7.82 KBgraber
#14 2982968-core-views-simplify-date-filter-14.patch7.82 KBgraber
#12 2982968-core-views-simplify-date-filter-12.patch7.82 KBgraber
#5 Capture du 2018-07-05 15-50-58.png25.93 KBq__nt_n
#2 Screenshot from 2018-07-04 19-23-14.png59.69 KBpanagiotisp
Screen Shot 2018-07-02 at 11.00.35.png19.86 KBjohn cook
Screen Shot 2018-07-02 at 11.00.01.png19.02 KBjohn cook
Screen Shot 2018-07-02 at 10.54.58.png180.91 KBjohn cook

Issue fork drupal-2982968

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

John Cook created an issue. See original summary.

panagiotisp’s picture

StatusFileSize
new59.69 KB

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

q__nt_n’s picture

#2 I'm agree with you but in which case do we want a relative text string ?

q__nt_n’s picture

Issue tags: +DevDaysLisbon
q__nt_n’s picture

StatusFileSize
new25.93 KB

I think, we must have a datepicker in filter date field (cf screenshot).
datepicker
What do you think?

john cook’s picture

@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:

  1. the date format – this is a bug
  2. have the field as a date picker – this would be a feature request

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.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.

vensires’s picture

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

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

graber’s picture

Component: datetime.module » views.module
Status: Active » Needs review
StatusFileSize
new7.82 KB

Here'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.

Status: Needs review » Needs work

The last submitted patch, 12: 2982968-core-views-simplify-date-filter-12.patch, failed testing. View results

graber’s picture

Status: Needs work » Needs review
StatusFileSize
new7.82 KB

One more try..

graber’s picture

StatusFileSize
new7.82 KB

Status: Needs review » Needs work

The last submitted patch, 15: 2982968-core-views-simplify-date-filter-15.patch, failed testing. View results

graber’s picture

Status: Needs work » Needs review
StatusFileSize
new11.93 KB

After making sure FilterDateTest passes locally..

Status: Needs review » Needs work

The last submitted patch, 17: 2982968-core-views-improve-date-filter-17.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

graber’s picture

Status: Needs work » Needs review
StatusFileSize
new11.97 KB

Overriding the query() method was not an option as datetime and datetime_range filters extend this class.

graber’s picture

Nice. Including a patch for 8.9.x as well.

snehalgaikwad’s picture

StatusFileSize
new140.12 KB
new53.88 KB

Tested patch #19 on 9.1.x and applied cleanly. Working correct for date filter with offset in views. Attaching screenshots below for reference.

bucefal91’s picture

Status: Needs review » Needs work
StatusFileSize
new211.92 KB

I'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").

graber’s picture

Status: Needs work » Needs review
StatusFileSize
new253.09 KB

Thanks 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?

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

juanolalla’s picture

Status: Needs review » Needs work

I'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 $component with 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.

douggreen’s picture

The 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:

  1. The 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 try strpbrk($form['value'][$component]['#default_value'], '/.,') !== FALSE
  2. Can we assume Y-m-d? Won't we need to get the sites (or fields) date format?
  3. We don't need to do isset() checks with empty(), as empty() covers this.
  4. Why is defineOptions() removed?
douggreen’s picture

StatusFileSize
new11.1 KB

Here's a straight re-roll for 9.1.x and 9.2.x.

douggreen’s picture

And this has the problems that @juanolalla mentions above. It's altering the wrong elements (at least on 9.x).

nishat ahmad’s picture

nishat ahmad’s picture

StatusFileSize
new11.97 KB
anmolgoyal74’s picture

Status: Needs work » Needs review
StatusFileSize
new11.7 KB

Re-rolled for 9.2.x

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rwohleb’s picture

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

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

khiminrm’s picture

Updated the patch to last Drupal 9.4.x

Status: Needs review » Needs work

The last submitted patch, 35: 2982968-core-views-improve-date-filter-34.patch, failed testing. View results

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

benstallings’s picture

Status: Needs work » Needs review
StatusFileSize
new8.12 KB

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

sonam.chaturvedi’s picture

StatusFileSize
new61.6 KB
new62.65 KB

Verified 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:
bef 38

After Patch:
aftr38

RTBC +1

benstallings’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, @sonam.chaturvedi !

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: 2982968-core-views-improve-date-filter-38.patch, failed testing. View results

mradcliffe’s picture

+++ b/core/modules/views/src/Plugin/views/filter/Date.php
@@ -13,37 +13,52 @@
-    $options['value']['contains']['type']['default'] = 'date';

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

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.

yes_max’s picture

Warning: 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: Exception

I applied my changes to #38 to fix the warning.

graber’s picture

I 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 :(

junaidpv’s picture

Status: Needs work » Needs review
StatusFileSize
new9 KB

Building on #44. Adding similar improvement as #38 but to the datetime filter in core datetime module.

junaidpv’s picture

Try fixing CCF.

smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for tests and issue summary update which still need to happen plese.

Also please include interdiffs with patches.

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.

junaidpv’s picture

Exactly same as #47, just re-rolled for 10.1.x

s3b0un3t’s picture

Hello,

I tried to apply patch on comment #50, but it's thrown an error in datetime module :

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

Without this warning, the patch works perfectly on Drupal 10.2.2!

prudloff’s picture

Starting with #38, the patch always adds +1 day to 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.

junaidpv’s picture

Just a re-roll of #50 for 10.3.2

Hopefully, we need to address #51 and #52.

plopesc’s picture

Added new version of the patch against 10.3.2 where it adds support also for missing max value.

sunnygambino’s picture

Patch 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. :)

johnv’s picture

Title: Exposed views date filter with offset not working as expected » Exposed filter for date field with offset not working as expected
johnv’s picture

Title: Exposed filter for date field with offset not working as expected » Date Exposed filter with offset not working as expected

The 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'

niranjan_panem’s picture

StatusFileSize
new2.52 KB

Below is the patch, in drupal 11

s3b0un3t’s picture

Hello @niranjan_panem,

You're patch for Drupal 11 is incorrect. There are comments conflict into your patch:

+<<<<<<< Updated upstream
     // This is safe because we are manually scrubbing the values. It is
     // necessary to do it this way because $a and $b are formulas when using an
     // offset.
+=======
+
+    /*
+    This is safe because we are manually scrubbing the values.
+    It is necessary to do it this way because $a and $b are formulas when using an offset.
+     */
+>>>>>>> Stashed changes

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

ressa’s picture

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

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

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.