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.
Is there a way to make the contextual filter (argument) filter by greater than and lower than, better yet if given an excluded NID take the date field from that node and use it for this comparison?
The use case is the following:
Say you have a list of bulletins (content type) and you want to create a block on the bottom of that bulletin page to show the 4 previous bulletins.
Comment | File | Size | Author |
---|---|---|---|
#28 | date-lt-ops-contextual-arguments-1417886-27.patch | 3.2 KB | rwaery.11 |
| |||
#26 | less_than_operator-1417886-26.patch | 3.23 KB | Valentine94 |
#23 | date-lt-ops-contextual-arguments-1417886-23.patch | 3.23 KB | timwood |
#18 | 1417886-18.patch | 3.23 KB | ultimateboy |
#14 | date-1417886-14.patch | 3.22 KB | dealancer |
Comments
Comment #1
rmontero CreditAttribution: rmontero commentedI have attached a patch that would allow you to specify comparison operators other than 'equals' when using a date_field contextual filter (argument).
Comment #2
ayalon CreditAttribution: ayalon commentedThis is an absolute genius feature.
It's now possible to pass an argument and display all nodes greater/lower than the passed argument (date).
I use this feature on a customer site.
Attached you find an updated patch for the latest date module.
@Karen: Could you have a look a this? It's really nice and I attached a screenshot for better understanding.
Comment #3
ayalon CreditAttribution: ayalon commentedWrong status, it's reviewed. The credits go to the thread starter.
Comment #5
derekw CreditAttribution: derekw commentedApplied the patch against -dev and flushed cache but the Operation option is not showing up on the form.
I'm trying to filter Content:Post Date, which requires adding Date:Date (Node) and then selecting the date field to use. So the contextual filter winds up looking like this: Date: Date (node) (Content: Post date)
Comment #6
kleinmp CreditAttribution: kleinmp commentedI applied the patch and it worked properly for me, but I did it on version 7.x-2.5.
In order to see the operations select list, I had to use the 'Only this field' option under 'Dates to Compare', which makes sense to me
Comment #7
tim.plunkettThis will break all queries NOT using this new feature. The trick was to set the default value to NULL.
Comment #8
Jarode CreditAttribution: Jarode commentedNeed these operators for all numeric values. (ex: nid, tid, file size, uid...)
Thank you.
Comment #9
ultimateboy CreditAttribution: ultimateboy commentedThis is a fantastic feature addition which I needed for a current project. I applied the patch in #7 and had a few issues with it - my updated patch solves the issues I was having.
Upon applying the patch, I did not see the operations select item. I tracked this down to the
#access
attribute of the new form item. Seems like the rest of the form items simply set#access
to$access
. This seems to be more consistent with the rest of the code and solved my issue of never being able to see the operations field.Secondly, I began getting a few notices/warnings, and several views simply stopped functioning as desired:
I solved this issue by removing the
return;
from the operations conditional. Returning seems to be unnecessary here. Removing thisreturn;
removed the notices/warnings and appears to make this patch act just as desired.Comment #10
andrew smith CreditAttribution: andrew smith commentedI have applied the above patch - date-1417886-9.patch - but cannot see the operators. I understand 'Operations' should appear below 'Dates to compare' - but it's not there for me. Am I doing something wrong?
Comment #11
malberts CreditAttribution: malberts commentedThe patch in #9 works for me.
However, it seems like this only works when you use "Date: Date (node)".
Adding the Date field directly does not give me the operators even when "Dates to Compare = Only this field".
Comment #12
David Stosik CreditAttribution: David Stosik commentedHello,
I've been working on such idea on my side, and produced the patch attached. I don't know where we're at concerning this issue, but that'll be a way to up the topic.
This patch provides a field "operator" in arguments of type "Date: Date (Entity name)", which will be enabled only if "Dates to compare" = "Only this field".
Feel free to comment and tell me ways to improve my code, I feel like I forgot cases.
Thanks,
David
Comment #13
dealancer CreditAttribution: dealancer commentedReviewed patch #9:
It works good. However we don't need to make operation field required, but need to add custom validation. There is a reason: if Dates To Compare field is set to Start/End Date Range, then this field is hidden, but still required and validation error is thrown.
Comment #14
dealancer CreditAttribution: dealancer commentedRe rolled patch #9.
Comment #15
dealancer CreditAttribution: dealancer commentedComment #16
lunk rat CreditAttribution: lunk rat commentedPatch in #14 works exactly as expected (I applied to 7.x-2.6 Date), is fantastically useful, and saved my butt on a project. Let me know how I can test/help to get this patch get committed, because this opens up a lot of great use-cases for filtering content by date.
Thanks ultimateboy and dealancer!
Comment #18
ultimateboy CreditAttribution: ultimateboy commentedRe-roll of #14.
Comment #19
vijaycs8518: 1417886-18.patch queued for re-testing.
Comment #21
vijaycs85Comment #22
podarokComment #23
timwoodRe-re-rolled of #18.
Comment #26
Valentine94Re-roll of #23.
Comment #28
rwaery.11 CreditAttribution: rwaery.11 commentedRe roll of #23