Problem/Motivation

I have a view which lists nodes by date. The view uses an exposed filter to let a user select a date range. If the min or max date is missing when the filters are applied, views returns all results.

Steps to reproduce

  1. Set up a view which exposed a date range filter (operator = "Is Between").
  2. If both Min & Max date are entered, results are returned within the range selected. If either are missing, all results are returned.

I would expect that Min; No Max would return all results from Min onward and Max; No Min would return all results up to Max. This has caused some of my users some consternation as we are using BEF to automatically trigger an update on filter changes.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

sroy26’s picture

StatusFileSize
new654 bytes

Please find the following patch for the issue.

sroy26’s picture

bhumikavarshney’s picture

Status: Active » Needs review
StatusFileSize
new45.36 KB

Patch #2 is nice. Thx for that.
It works for me.
Now, If both Min & Max date are entered, results are returned within the range selected. If either are missing, all results are returned this condition is working as expected.

mitthukumawat’s picture

Patch #2 applied cleanly on drupal 9.x.3-dev version for me and resolved the issue.
Thanks for the patch.

raphael apard’s picture

This is only working if we set MAX with empty MIN.
With MIN set and empty MAX, the condition is "BETWEEN MIN_DATE AND 0" so there is no result.

When only min is set, condition should be ">= $a" and not "BETWEEN $a AND $b"

hhvardan’s picture

The patch doesn't work if the min value exists and max is empty.

hhvardan’s picture

StatusFileSize
new1.31 KB
hhvardan’s picture

hhvardan’s picture

ranjith_kumar_k_u’s picture

StatusFileSize
new1.44 KB
new688 bytes

Fixed CS issues

Status: Needs review » Needs work

The last submitted patch, 11: 3202489-11.patch, failed testing. View results

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

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should 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.

golddragon007’s picture

The patch was not complete, so I added support for datetime module, and not between operations.
And added a related issue from the integer filter variant.

I removed this:

    // Although both 'min' and 'max' values are required, default empty 'min'
    // value as UNIX timestamp 0.
    $min = (!empty($this->value['min'])) ? $this->value['min'] : '@0';

As it makes no sense currently for me, in that moment and time we have a string date, and not a timestamp.
Originates from: https://www.drupal.org/project/drupal/issues/2627512

golddragon007’s picture

Issue tags: +Needs tests
golddragon007’s picture

StatusFileSize
new7.1 KB

Wrong condition fix when both times set.

dsdeiz’s picture

Status: Needs work » Needs review
StatusFileSize
new1.82 KB
new8.89 KB

Yeah, works on my end. I've added the tests.

dsdeiz’s picture

Status: Needs review » Needs work

The last submitted patch, 18: drupal-min-max-date-3202489-17-test-only.patch, failed testing. View results

dsdeiz’s picture

Status: Needs work » Needs review
vikashsoni’s picture

StatusFileSize
new25.72 KB

Applied patch #2 in drupal-9.3.x-dev applied successfully and fix the issue
Thanks for the patch
for ref sharing screenshot...

golddragon007’s picture

Status: Needs review » Reviewed & tested by the community

#18 tested and looks fine.

quietone’s picture

Version: 9.3.x-dev » 10.0.x-dev
Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests +Bug Smash Initiative

Thanks for fixing this.

This needs to be on 10.0.x now, changing version. The patch includes a test so removing tag.

A brief look at the patch and I notices these two items.

  1. +++ b/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php
    @@ -140,6 +140,37 @@ class FilterDateTest extends DateTimeHandlerTestBase {
    +      // Test offset for between operator when only min is set. 'Yesterday' and
    

    I think this should be max.

  2. +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php
    @@ -105,21 +105,33 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
    +      // Convert to ISO format and format for query. UTC timezone is used since
    +      // dates are stored in UTC.
    

    This comment should now be above the whole if block since it applies to all 3 paths.

I applied the patch to 9.5.x and did some testing. I added a date field to the article content type and then made a view with two exposed filters, one on the added date field and the other on the authored on field. I used devel generated content with authored dates going back for a year. I added a date to two node.
I did not do exhaustive testing. The date field appeared to work with offset and date. The authored on field worked with dates but failed with just a minimum offset. And min offset of '-3 days' failed to show any nodes but a min of '-3 days' and a max of '-2 days' would show the correct nodes.

Setting to NW.

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new8.8 KB
new1.78 KB

I tried to fix the changes mentioned in #24. Please have a look.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

The issue summary needs to be updated with proposed solution.
CI failures in #25

simobm’s picture

It's weird, i've tried all of the patches and none seems to works, i've tried on 9.3.22 , 9.4.11 and 9.5.3.

I'm getting the same results as @hhvardan and #Raphael Apard only max works alone, if i use only min i get no results.

camilo.escobar’s picture

This issue is also solved in https://www.drupal.org/node/2982968

s3b0un3t’s picture

Hello,

@camilo.escobar : I've try to use your patch in issue #2982968 but it's return an error.
I will add a comment on your issue in order to trace the error.

The patch #25 seems to work on Drupal 10.2.2.

Cheers.

Version: 10.0.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

khiminrm’s picture

Patch from #25 also works for me on Drupal 10.2.6

gun_dose’s picture

StatusFileSize
new8.8 KB

Updated patch that applies to Drupal 11.2

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.