views:

353

answers:

2

I'm struggling a little bit with a View that gets data from a node that uses two node reference fields, that points to the same type of content. From my path I'm getting one string as the argument, let's call it $fruit.

The query I would make if I was writing the query myself would be something like SELECT * FROM recipe WHERE (fruit_1=$fruit OR fruit_2=$fruit). My problem is that I don't get how to do this in a Drupal View. Any suggestions?

UPDATE: I finally solved what I've been trying to do. The solution for me was to use Panels together with a View using Views Or arguments, and setting both of the arguments values to the same using the first Panels argument. Not sure if I'm explaining either my problem or my solution properly, but if anyone would need a closer explanation, feel free to ask.

+1  A: 

I don't think there's a way to do the ... OR ... part of your query in the Views UI. However, you can make your own module and use the Views UI to generate the first part of the query, then use hook_views_query_alter in your module to just append WHERE (fruit_1=$fruit OR fruit_2=$fruit) to the end of the query.

More info on hook_views_query_alter here and here. Good luck.

theunraveler
+1  A: 

Views Or module addon: http://drupal.org/project/views%5For

It really ought to be included with Views by default because it is so powerful.

The 'begin alternatives' option essentially places a '(' in your query. The 'next alternative' option essentially places the ' OR ' in your query. The 'end alternatives' option essentially places a ')' in your query.

Views filters are setup as ' AND ' between each field, this adds that ' OR ' capability. So using your example your filters would be something like:

Content Type = Recipe
Begin Alternatives
Node Title = fruit_1
Next Alternative
Node Title = fruit_2
End Alternatives

Note that you can have several Or options by using more 'next alternative' filters, each places an ' OR ' between the other filters. If it's still confusing, check the query preview in your views pane after each change.

Kirk Hings
Nice find! (+1)
mac
I have been trying Views Or for some time, but I just don't seem to get how to do it. As long as I have one argument with a `Node ID validator`, it works as expected, but when I add a `Next alternative` and validate my second field against the same value (with `Share argument`), I get an empty `WHERE`, and an sql error. Not sure what I'm doing wrong
Marco

related questions