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.
Steps to reproduce:
Add filter for node creation date, select 'Between' operator and type something like
first day of last month
last day of last month
It will be interpreted as
AND (node.created >= -2678400) AND (node.created <= -86400)
because there is a base for calculating as relative date in op_between() function in handlers/views_handler_filter_date.inc:
function op_between($field) {
if ($this->operator == 'between') {
$a = intval(strtotime($this->value['min'],0));
$b = intval(strtotime($this->value['max'],0));
}
else {
$a = intval(strtotime($this->value['max'],0));
$b = intval(strtotime($this->value['min'],0));
}
It is fixed for me if I delete these zeros:
function op_between($field) {
if ($this->operator == 'between') {
$a = intval(strtotime($this->value['min']));
$b = intval(strtotime($this->value['max']));
}
else {
$a = intval(strtotime($this->value['max']));
$b = intval(strtotime($this->value['min']));
}
These zeros also present in 6.x-3.0-alpha3.
Comment | File | Size | Author |
---|---|---|---|
#44 | 1182256_44.patch | 5.47 KB | Yasser Samman |
#42 | interdiff_36-41.txt | 2.96 KB | shubham.prakash |
#41 | 1182256-41.patch | 3.77 KB | shubham.prakash |
Comments
Comment #1
dawehnerCan you please have a look at see whether it works for you in the dev versions?
Comment #2
artis CreditAttribution: artis commentedpatch against 6.x-3.x-dev
Comment #3
artis CreditAttribution: artis commentedpatch against 7.x-3.x-dev
Comment #4
dagmarNice catch. I tested your patch and works as expected. However reading the code of views_handler_filter_date.inc I found this:
And after test the same "first day of last month" I noticed that it doesn't work as expected.
Please provide a patch containing the fix for op_simple too. Thanks.
Comment #5
dawehnerChanging the base of strtotime is also not right because this would kill a lot of other things.
The filter is not designed for expecting this kind of input if you use "A date in any machine readable format."
Maybe a third value type could be introduced to make it a bit easier to understand.
If this is fixed it should be simpletested as well. Updating the status, so the testbot is runned here.
Comment #7
das-peter CreditAttribution: das-peter commentedAtm. I can't imagine a scenario in which I need the base 0 for the relative date calculation. For me it seems 0 is used to be able to work with
***CURRENT_TIME***
for offset values.But what if we change to code to something like this:
That way offset values rely on
$query_substitutions['***CURRENT_TIME***']
and in the normal case$query_substitutions['***CURRENT_TIME***']
isREQUEST_TIME
- which means a value likenow
gets the current timestamp and not 0.Comment #8
das-peter CreditAttribution: das-peter commentedAfter a short chat with Daniel I decided to create a patch and see if all current tests pass ;)
Values like
first day of last month
/last day of last month
should now return a usable timestamp.Feedback very welcome!
Comment #9
tim.plunkettSo this doesn't break any existing tests, but it definitely needs new ones before being committed.
Comment #10
das-peter CreditAttribution: das-peter commentedI tried to remember why I had to change this but I can't reproduce it anymore :|
The current code will produce
{field} > {CURRENT_TIME}+0
fornow
as long as you define the value as an offset.Only "advantage" of the patch would be a potentially easier to understand construct because it removes to need to handle "offset" values in a special way.
This also means that the UI could be adjusted because there's no need to declare the type of the date.
What kind of new tests? As far as I can see the coverage is quite good for this.
Comment #11
windmaomao CreditAttribution: windmaomao commentedI had the same issue, seems to me the bug is because
doesn't return the correct interval
#8 works for me !!
Comment #12
jbenezech CreditAttribution: jbenezech commentedSame problem here trying to filter node created between today and tomorrow. #8 worked for me
Comment #13
stella CreditAttribution: stella commentedUpdated patch, no changes other than the addition of some new tests
Comment #15
stella CreditAttribution: stella commentedPatch reroll against latest dev
Comment #16
mariancalinro CreditAttribution: mariancalinro commentedI tested the patch in #15, and it fixes the issue on both between and simple conditions.
Comment #17
fonant CreditAttribution: fonant commentedPatch in #15 works here so that "midnight" results in the previous midnight, whatever time of day it is currently, as you'd expect from a php relative date.
I haven't done exhaustive testing, but this seems to be quite an important fix. Without it common relative date strings like "yesterday" and "today" just don't work the way they work in PHP's strtotime().
Comment #18
vadym.kononenko CreditAttribution: vadym.kononenko commentedI've tried to add views filter between two dates: 1) 'Last Monday -1 week', 2) 'Last Monday' to get entities for previous week.
Generic views behaviour calculates for me wrong timestamps. It became shifted and instead of get two mondays return me just thursdays.
I think it is because strtotime uses calendar for 01.01.1970 day and it produces an logical error.
Patch #15 works for me. Thank you a lot!
Comment #19
olli CreditAttribution: olli commentedI think the ***CURRENT_TIME*** is needed for caching so we can't replace it here, right?
Comment #20
fonant CreditAttribution: fonant commentedWould be really nice to get this patch into a supported release. The latest Views security release doesn't include this important fix, so the patch has to be re-applied.
This fixes a nasty bug that's difficult to spot and diagnose if you're using relative date filters in Views!
Comment #21
dawehner@olli
I think the query cache doesn't change. The following code generates parts of the result key:
This piece of code triggers the
query_alter()
hook and based upon that always had the ***CURRENT_TIME*** replace.This means that those queries are already not cacheable and you better write your own cache plugin, if you need them to be. (I hope all this is true)
Given that this issue solves a bug, let's get it in. It would be great if someone could evaluate whether we need this in 8.0.x as well.
Committed to 7.x-3.x and pushed
Comment #22
jibranHere is the patch without tests.
Comment #23
dawehnerThat part seriously needs tests indeed but the port is looking fine so far!
Comment #25
kenorb CreditAttribution: kenorb commentedComment #26
LendudeAdded some tests. Interdiff is the test-only patch.
Comment #34
PCate CreditAttribution: PCate commentedRan into this issue today. I think the patch needs a re-roll.
Comment #35
Yasser SammanRe-roll.
This is kind of an important issue I don't know why it's being ignored.
Comment #37
Yasser SammanMissed including views.views_execution.inc file to be able to use views_views_form_substitutions
Comment #38
PCate CreditAttribution: PCate commentedThank you @Yasser Samman for the patch re-roll. I applied it and it fixed the issue.
Comment #39
PCate CreditAttribution: PCate commentedComment #40
LendudeThanks for working on this! One thing I see though:
views_views_query_substitutions() is a hook implementation so we shouldn't be calling that directly.
We should probably \Drupal::moduleHandler()->invokeAll('views_query_substitutions', [$this->view]); (or better yet inject the moduleHandler in the plugin if possible)
Comment #41
shubham.prakash CreditAttribution: shubham.prakash at OpenSense Labs commentedDid the changes suggested in #41
Comment #42
shubham.prakash CreditAttribution: shubham.prakash at OpenSense Labs commentedComment #43
Lendude@shubham.prakash thanks for the quick pick up, couple of things:
We can lose the includes when using the module handler to invokeAll
Did you try using Dependency Injection (like I hinted at in #40) by implementing ContainerFactoryPluginInterface on the plugin instead of using \Drupal? If not, that would be the preferred way of doing this, unless there is some reason not to do this.
Comment #44
Yasser SammanThanks @Lendude.
Using dependency injection to be able to use module_handler service.
I just didn't have enough time the last time I did it :p
Comment #46
Yasser SammanOk..
So the test failed because the datetime module is extending our Drupal\views\Plugin\views\filter\Date that we modified. Possibly other modules are extending the same plugin too.
What do you think we should do? Should we keep using \Drupal::moduleHandler() insead of dependency injection for the time being?
Comment #48
marcvangendClosing this in favor of #2647292: Date/time Views filter tries strotime() relative to Unix epoch. The solution in that issue is almost identical to the patch here. Reviews are appreciated.