Drupal 7 views - how to combine a contextual filter with a regular filter (with OR)? - drupal

Ok, so I have a "playlist" content type, with a node reference field and a boolean field called "always display this playlist".
I have a "playlists" view with a contextual filter based on the node reference. So, when I go to "/playlists/35", it shows all the playlists that reference node 35.
All good so far.
One extra requirement is that the view needs to also display all the playlist that have the "always_display_playlist" field set to 1 (that's the boolean field).
So basically the view needs to display playlists that match the contextual filter OR that have the boolean field set to 1.
So far I have failed rather miserably in making that work. The contextual filter works great, but of course it will only display playlists that have the node reference matching the argument. How can I combine the contextual filter with another regular filter, but with "or"?
Thanks a lot for any clue or idea!

Option 1
Consider the Views Contextual Filters OR module, which only has a dev version today. Excerpt from its project page:
... provides a views plugin which modify query to support OR conditions for contextual filters.
Option 2
Otherwise the Views EVI (Exposed Value Injector) module, which only has a beta version today. Excerpt from its project page:
This module solves the fundamental and longstanding problem (#357082: Pull filter value from an argument? & Itangalos Sandbox) of views argument filters (=contextual filters): they are far less potent than regular filters.
It does this by allowing to wire argument tokens to exposed filters and optionally hide them. It also allows new use cases, think: use one exposed widget for two filters.
Credits (for Option 2): Hubert

If you click on the dropdown beside filters you could potentially use two different filter groups, one that corresponds to the contextual filter and manage it with views filter harmonizer and the other group with the boolean filter and have the OR between them.

Related

Filtering a glossary view

I have a glossary view, which is just the one that comes with Views. I want to allow users to filter the results by taxonomy terms (dropdown) and also by title (search field). The problem I'm running into is that when I click on a glossary letter (e.g. X) then I try to type in a search term that begins with something other than X (e.g. Cat) I get no results. The title search filter is basically just searching within the selected letter in the Glossary. So if I'm on the X page and I search for Cat nothing is found. If I would search for X-ray then that would appear.
Is there any way to make the title filter relatable to the entire view result and not just things that begin with the glossary letter than I have selected?
In the attachment of the view I've tried turning on the "Inherit exposed filters" option but that does not help.
This happens because the view has already a Contextual Filter in use that is filtering the query from the path arguments. That means that the exposed filter is adding a filter after the main filter from the path.
First of all, enable the "Show the SQL query" option on your views settings (admin/structure/views/settings). This way you will be able to see immediately what is going on.
Furthermore, you can set the contextual filter for the title for the Page views as
"WHEN THE FILTER VALUE IS NOT IN THE URL -> Display all results for the specified field" because by default it shows titles beginning with letter "a" in the first load.
So to achieve this you should remove any path argument that exists in the path due to the contextual filter (eg for the domain.com/l?title="some title here" you must have domain.com/?title="some title here", without the "l").
You could do this with the following methods:
Override Views Contextual Filter in View Exposed Form Validation
Redirect the page removing the last path argument, eg with Rules module or a drupal function
Use the fresh filter_harmonizer module!
And the winner is probably the filter_harmonizer module.

Drupal 7 Views: Combine exposed filter with contextual filter?

Is there a way to make an exposed filter and contextual filter play nice with each other? I have the title field set as my exposed filter and then I also have a contextual filter in glossary mode. When I do a search with my exposed filter it works normally until I click on one of the letters for my contextual glossary.. then I get no results.
For example when I first come to my page it shows me that there are 35 records for the letter P. if I do a search for the word Christmas I get some results on the page but the number of records for the letter P still says 35. When I click on the letter P I get 0 results because the view is still being filtered by the word Christmas.
Is there anyway to make these two things play nicely with each other to get some results that make a little more sense? It would be nice if I clicked on a glossary letter than the exposed title filter would be reset.
Enable the filter_harmonizer module (Actually this module was publish after the question date)
(optionally) Disable AJAX for the views and set the contextual filter:
"WHEN THE FILTER VALUE IS NOT IN THE URL -> Display all results for the specified field" because by default it shows titles beginning with letter "a" in the first load.
You should try with changing the 'AND' for your filters to 'OR'
By default, if you check the added filters, they are added with 'AND' condition. So, each time you filter the result, its 'AND'ed with previous query.
Regards.

Add view filter to retrieve content associated with one or more Taxonomy Term IDs

I have created a Drupal 6 view which has two "Taxonomy: Term" (Term ID) filters, for two different taxonomy terms. I need to create a feed that pulls content that is associated with a OR b, or that is associated with a AND b. Currently, nothing is displayed because it seems that the logic of two filters is AND, not OR.
How do you pull content that has the logic OR with Drupal views?
It does work if there is only one filter, but as soon as I add a second filter, zero records are found.
The OR filter is not built in to Views2. For ways to get around this, see http://groups.drupal.org/node/16173. And this old post might help as well OR operator in Drupal View Filters

Drupal Views and exposed filters - how to reset optional drop-down list filter, or allow "all" selection

I have a view with a filter by country. Country is an optional CCK text field with drop-down list selection from a predefined list.
When I expose the field as an optional view search filter, I can select a country on the filter form just fine, but how can I handle no selection? I want the user to be able to "un-select" any previously selected value so that the query runs for all countries (or, to be more precise, does not constrain the results by country). I cannot add "all" as a value to the CCK field for obvious reasons. Selecting all countries in the drop-down on the search filter is not very user-friendly and causes the page to time out (there are over 200 countries in the list, and I can only imagine the resulting SQL query). Whats the best way to handle this?
I found this View Filters Reset hack which could be helpful. The desire to memorize the last selection adds another complication - when configured in the View properties, and once the (persistent) cookie is set, there is no way to get rid of the selection (short of manually deleting the cookie).
The answer was to check "Force Single" on the filter properties. This made <any> show up as a selection in the country filter drop-down. It kind of makes sense, because <any> in a multiple-selection list could be seen as ambiguous. Still, I can think of valid reasons why a multiple selection filter might be optional. Drupal surprises me every day.

Views 1: Filter by custom table/field (or using Argument Hand. Code)

I have a page which should list nodes. The views is called from a locality page (a taxonomy term page). What I need is almost the same as using the Taxonomy: tid in arguments and passing the tid.
I can't use the term_node table, as (for other reasons) we have a custom table term_node_hierarchy (with nid and tid only). The table term_node_hierarchy is like term_node but also saves the tid of the parents (from an "external" code)
I've been looking for options but still no joy.
Currently I'm building an array of the nid's that should be displayed on the current page, and passing them like print views_build_view('embed', $view, $matching_nids); but the Argument Node: ID states This argument is a single Node ID. As said, only the first node is displayed when printing the views. It would be great if it could filter on more than one nid.
I'm open to any kind of suggestions on how to do this.
Thanks
You could create your own module for this. You could populate the $page_content variable with the results of your own custom query where you allow the user to sort against multiple nids. You could do this a number of different ways. You could display a list of the existing nids with corresponding checkboxes, so that, when the user clicks submit, all the nids that match the selected checkboxes get used in the query. Then you just display the result of that query. That's the easiest way I can think of to offer that degree of flexibility.

Resources