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

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.

Related

How to show more than 100 items in dropdown options using projections list

I have a model "address", and one field is country, in the create/edit form I use dropdown to let user select from country list, country list is store in another model called "listCountry", there are 249 countries in total, but in the dropdown options, only first 100 countries shown.
My questions and concern are:
In the options setting, I use #datasources.listCountry.items..Country, and I know there is a default pagesize of 100, but I tried to change that property pageSize as suggested by google's doc, I added to Event Load of that dropdown app.datasources.listCountry.query.pageSize = 300;, but it doesn't work, any suggestion?
I tried Search Box instead of dropdown as workaround, it works, but it requires to set a relation between Address model and listCountry model, which is not required when I use Dropdown. I'm wondering which is the best practice to store an option list, like in this case when we need give an list options such as country for an address form, is that relation setting necessary?
Thanks in advance.

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

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.

Advanced Custom Fields - Filtering a Post Object Field by previously chosen Taxonomy

I have a custom post type called Event in which I want to add a location chosen from a large list of locations. To make this process easier I want to filter these locations by a category called locality.
To achieve this I have created a custom post type called Locations and a custom taxonomy called Locality. A field group holding two dropdowns one for Locality (Taxonomy) and another for Locations (Post Object) has been added to each event.
What I haven't managed to achieve is the filter of locations by the locality chosen.
I don't believe you can dynamically filter the choices of one custom field based on the current choice of another, and change that choice on the adminend. You can filter the choices that show up in your fields with a query modification that's very similar to WP_Query, and I'm pretty sure that can't be done on the fly.
I would almost cheat and instead of making posts with taxonomy assigned to them, include both the locality and the location in the post name itself.
So say, post [Museum of London] with taxonomy [London]
would become
[London][Museum of London]
[London][Hyde Park]... etc
or something similar. I'm presuming you only want to assign one location to any specific locality...
Then I'd create a relationship field and whenever I'm looking for Museum of London I'd type
London Muse...
It looks like this question is stale by now, but you would make your life easier if you added two custom fields to Event - location and locality. That way you don't have to join two tables and can easily use WP meta_query to filter posts.
What i understood is that you want that whenever you select the locality the location for it should be shown there and at last you can filter Events with locations.
=> To set location you assigned two drop-down for locality and location. You should use ajax. when i select any locality than the next drop-down has the options for the selected locality.
=> To add value in event post: you might have two options
i) you may add location as taxonomy and search using "tax query" to filter
ii) add location as parent of event and search "child of" in wp_query to filter
You have two options:
Program custom js to filter the locations field onchange of the localities field. This would not be so hard to do, because ACF sets the field id's for every field in their html. You can enqueue this js script in an admin hook (probably admin_enqueue_scripts, check this solution) and filter for the custom post type.
It can all be done in the ACF gui, but it's a bit of a trick, also because of the fact that the conditional logic for acf's taxonomy field is lacking. What you have to do is create as many Location fields in the Event acf fields as you have localities. Then set these locations to specific localities. Now comes the weird part. You would want to set the conditional logic to display if the locality is e.g. Russia, but it lacks a 'specific value' field. So you have to set two rules, one to set the 'greater than' and one to set the 'lesser than' for the id of the locality. . This will be a pain if you have a lot of localities, but it might be a good option if your localities list has no more than a few items. If you name the location fields all 'location' than you can just get the value with get_field('location'). I tested this and there is no conflict in naming all the location fields location. ACF has done a pretty decent job there, having unique names for these fields also to be able to name these fields as you whish.

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.

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