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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rmontero’s picture

Title: Lower than operator for arguments » 'Less than' operator for contextual arguments
FileSize
5.58 KB

I have attached a patch that would allow you to specify comparison operators other than 'equals' when using a date_field contextual filter (argument).

ayalon’s picture

Status: Active » Needs review
FileSize
85.55 KB
2.96 KB

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

ayalon’s picture

Status: Needs review » Reviewed & tested by the community

Wrong status, it's reviewed. The credits go to the thread starter.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 1417886-date-operators-for-contextual-arguments.patch, failed testing.

derekw’s picture

Applied 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)

kleinmp’s picture

I 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

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
2.71 KB

This will break all queries NOT using this new feature. The trick was to set the default value to NULL.

Jarode’s picture

Need these operators for all numeric values. (ex: nid, tid, file size, uid...)

Thank you.

ultimateboy’s picture

FileSize
2.66 KB

This 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:

Notice: Undefined property: date_sql_handler::$placeholders in date_views_argument_handler->query() (line 176 of [...]/modules/contrib/date/date_views/includes/date_views_argument_handler.inc).

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in date_views_argument_handler->query() (line 176 of [...]/modules/contrib/date/date_views/includes/date_views_argument_handler.inc).

I solved this issue by removing the return; from the operations conditional. Returning seems to be unnecessary here. Removing this return; removed the notices/warnings and appears to make this patch act just as desired.

andrew smith’s picture

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

malberts’s picture

The 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".

David Stosik’s picture

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

dealancer’s picture

Status: Needs review » Needs work

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

dealancer’s picture

FileSize
3.22 KB

Re rolled patch #9.

dealancer’s picture

Status: Needs work » Needs review
lunk rat’s picture

Patch 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!

Status: Needs review » Needs work

The last submitted patch, date-1417886-14.patch, failed testing.

ultimateboy’s picture

Status: Needs work » Needs review
FileSize
3.23 KB

Re-roll of #14.

vijaycs85’s picture

18: 1417886-18.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 18: 1417886-18.patch, failed testing.

vijaycs85’s picture

Issue summary: View changes
Issue tags: +Needs reroll
podarok’s picture

timwood’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
3.23 KB

Re-re-rolled of #18.

Status: Needs review » Needs work

The last submitted patch, 23: date-lt-ops-contextual-arguments-1417886-23.patch, failed testing.

Valentine94’s picture

Status: Needs work » Needs review
FileSize
3.23 KB

Re-roll of #23.

Status: Needs review » Needs work

The last submitted patch, 26: less_than_operator-1417886-26.patch, failed testing.

rwaery.11’s picture