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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TuWebO created an issue. See original summary.

TuWebO’s picture

Status: Active » Needs review
FileSize
1.99 KB

Patch added, please review.

Lendude’s picture

Status: Needs review » Needs work

I like this idea, but I think some things are missing right now.

At least:

  • This only makes sense for the exposed filter, so I would set a #state for this.
  • This needs a config change in the filter settings I think (and this would need an update path).
  • This needs tests.

Not having this feature leads to unclarity like this #2832652: Date between filter is filtering wrong

TuWebO’s picture

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

  • Make functionality for opBetween
  • Add test
TuWebO’s picture

Hello,
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?

Lendude’s picture

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

TuWebO’s picture

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

  1. datefrom: 20-12-2016
  2. dateto : 22-12-2016

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:

  1. datefrom: 20-12-2016
  2. dateto : 21-12-2016

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?

mpdonadio’s picture

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

TuWebO’s picture

Status: Needs work » Postponed

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

bkosborne’s picture

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

bkosborne’s picture

Title: Add time to date filter » Add time to views date filter

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.

adolfo81’s picture

Hi,
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.

wouters_f’s picture

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

wouters_f’s picture

Version: 8.3.x-dev » 8.4.x-dev
mpdonadio’s picture

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

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

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

chrotto’s picture

Is this still ongoing?

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

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

akalam’s picture

The patch does not apply agains 8.4+

akalam’s picture

Patch attached against 8.4.5, based on #4 and #13

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.

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.

dww’s picture

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.

gnuget’s picture

FileSize
5.01 KB

I rerolled the patch for Drupal 9.X in case someone need it.

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.

ariane’s picture

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

whiz11’s picture

I am getting issues with schema when running tests. I updated the patch.

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.

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.

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.

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.