Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
I would like to add a time to date filter, so we can have granularity when filtering date fields.
Use case filter between 2016-01-01T00:00:00 and 2016-01-01T23:59:59 (as exposed filter).
It could be solved with two exposed filters: Start date - End date.
Views filter uses 00:00:00T as default, with this patch it will be selected by UI (attached screenshot).
This way the user could select same date for both filters (more intuitive) and the second filter will show items before 23:59:59 (second attached screenshot)
I will add a patch for it.
Comment | File | Size | Author |
---|---|---|---|
#31 | views-add-time-to-date-filter-2832058-31.patch | 5.59 KB | whiz11 |
#28 | drupal-9.1-2832058-28.patch | 5.01 KB | gnuget |
#21 | views-add-time-to-date-filter-2832058-21.patch | 5.15 KB | akalam |
#14 | views-add-time-to-date-filter-2832058-14.patch | 2.9 KB | wouters_f |
#13 | views-add-time-to-date-filter-2832058-5.patch | 5.18 KB | adolfo81 |
Comments
Comment #2
TuWebO CreditAttribution: TuWebO at Metadrop commentedPatch added, please review.
Comment #3
LendudeI like this idea, but I think some things are missing right now.
At least:
Not having this feature leads to unclarity like this #2832652: Date between filter is filtering wrong
Comment #4
TuWebO CreditAttribution: TuWebO at Metadrop commentedHi @Lendude,
Thank you very much for reviewing.
I've added a new patch. It has the #states, so the append time will only shows when exposed checkbox is checked. Also add functionallity for the opsimple to work properly when append time has value.
This patch is not complete but we can track the work we are doing here and discuss if the code is ok.
Things TODO:
Comment #5
TuWebO CreditAttribution: TuWebO at Metadrop commentedHello,
I am confused about where I should add the test.
I've been looking at /core/modules/datetime/src/Tests/Views/FilterDateTimeTest.php but the code in this issue is in /core/modules/views/src/Plugin/views/filter/Date.php.
Looking at the tests in views, I can see /core/modules/views/src/Tests/Handler/FilterDateTest.php that uses
test_filter_date_between view for testing (it looks like the class name is wrong, since it is FilterDateTest and it looks like it should be FilterDateBetweenTest).
So what should I do?
Add another test view like /core/modules/views/tests/modules/views_test_config/test_views/views.view.test_filter_date_between.yml and named something like views.view.test_filter_date_simple.yml and use this one for create a /core/modules/views/src/Tests/Handler/FilterDateOpSimpleTest.php?
Could anyone give me any advice here?
Comment #6
LendudeI thought this looked familiar and @mpdonadio pointed me to #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter, that seems to add this in a more generic way. @TuWebO do you think that issues covers what you were looking for?
Comment #7
TuWebO CreditAttribution: TuWebO at Metadrop commentedHello @Lenlude,
I'm not pretty sure if it covers it.
As a use case, lets say that we have two exposed date filters, datefrom and dateto.
As a user I want to filter some entities for dates 20-12-2016 and 21-12-2016. Currently (and apparently also if we apply the patch mentioned your comment #6) I have to set the filters as follows:
I think it is not intuitive, I'm not an expert on UX but this is the opinion that I've got from some users.
With the patch in #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter, if I don't get it wrong (I didn't apply the patch) users will have to enter the time, but that's also a little wierd.
I would like the users to set these dates instead without having to set the time:
I think this patch will cover it and maybe it is not incompatible with the issue that you mentioned in #6, since the site builder could set a date widget (without time) and also append a custom time to it (so the final user will not have to add it by himself).
What do you think?
Comment #8
mpdonadioMy suggesting would be to postpone this on #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter to see where the final version of that lands, and then if it isn't adequate, do this as a new Filter plugin in Datetime Extras instead of the core filters. That would prevent the need for the update path, allow us to iterate quicker on the functionality, and also allow us to handle the balance between generic and specific functionality a little better.
Thoughts?
Comment #9
TuWebO CreditAttribution: TuWebO at Metadrop commented@mpdonadio looks good suggestion.
I'll set it as Postponed, you guys feel free to change it to Project Datetime Extras, I think it is a good idea.
Thanks for your rapid responses and reviews.
Comment #10
bkosborneAlso note that the filter for the Datetime fields (
Drupal\datetime\Plugin\views\filter\Date
) extends this one, and I'm pretty confident that this patch would then break datetime filters because it just tacks on the time to the end.Comment #11
bkosborneComment #13
adolfo81 CreditAttribution: adolfo81 commentedHi,
I try to patch views with views-add-time-to-date-filter-2832058-4.patch in Drupal 8.3.0 core but i cant. I have to adapt the patch to new code. I tested and works.
Comment #14
wouters_f CreditAttribution: wouters_f for VRT commentedI had a similar problem. For filtering users with birthdays (only this month - or this day of the month) I wanted to filter fields of type date.
I tried it myself. The code in the path attached is working for me, but maybe belongs somewhere else.
Please advise where I should put this code?
Should I maybe create a different issue for this?
*one weird thing i noticed is that for dates the NumericFilter is used. Should there not be something as a Datefilter or something?
Comment #15
wouters_f CreditAttribution: wouters_f for VRT commentedComment #16
mpdonadio#14, thanks for the patches. We are currently postponed on #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter, to see how we end up solving that.
To answer your question, though, the main date related plugins are in Drupal\views\Plugin\views, and they extend the Numeric ones. The datetime field plugins are in Drupal\datetime\Plugin\views, and they extend the main date ones.
Comment #18
chrotto CreditAttribution: chrotto commentedIs this still ongoing?
Comment #20
akalam CreditAttribution: akalam commentedThe patch does not apply agains 8.4+
Comment #21
akalam CreditAttribution: akalam at Metadrop commentedPatch attached against 8.4.5, based on #4 and #13
Comment #26
dwwAdding #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter as the blocking parent issue for better visibility.
Comment #28
gnugetI rerolled the patch for Drupal 9.X in case someone need it.
Comment #30
ariane CreditAttribution: ariane commentedHi there,
I´m not sure what I´m doing wrong, but after applying the patch of #28, the filter doesn´t work properly.
I have two independent date filters, from (>=) and to (<=). Am I suppose to add in to filter an hour like 23:23:59 to the input Append time, right?
After that, I search through the filter in my view but in the url appears like this:
Thanks in advance.
Comment #31
whiz11 CreditAttribution: whiz11 commentedI am getting issues with schema when running tests. I updated the patch.