I have an exposed filter in my View that allows users to choose between two different content types. The default value for the exposed filter seems to be chosen by whichever content type comes first alphabetically. So, if I let users choose between "Band" and "Song" content types, the default is "Band". I want to change this default value to a different content type ("Song" in the example here). I can't find an option in the Views interface for this. Do I have to do this via hook_form_alter
? If so, how exactly would I do that?
5 Answers
So, I tried the way that @David Lanier suggested, but couldn't quite wrap my head around it. So I went back to Drupal basics and tried it from the hook_form_alter()
approach. After some troubleshooting, I also found a post about changing default Views filter values. I took the same approach and got it working. Below is the code a used, with a couple of modifications to take out my site-specific stuff. As always, be sure to replace "modulename" with the name of your module.
function modulename_form_alter(&$form, $form_state, $form_id) {
// is this the right form?
if($form['#id'] == 'name-of-the-views-form-displayed') {
if (empty($_GET['label-of-the-filter'])) {
$form_state['input']['label-of-the-filter'] = 'default-value-for-filter';
}
}
}
Just replace name-of-the-views-form-displayed
with, you guessed, the name of the views form you want to change. The easiest way to find this is to inspect the source of the View in the browser (I love Firebug for this) and find the form
element, then grab it's id
attribute. To find the name of the filter and the value you should set, change the filters and perform the "search". Look in the address bar and you'll see the values you need to use here as parameters in the url.
Hope this helps get others around this issue too!
-
Sorry for thread necromancy, but I tried your solution and it indeed changes what is preselected in the input field -- but the actual view is rendered already (non AJAX view). Ended up adding a JS snippet in the footer of the View that picks the <Any> value if admin is selected, and then submits the views form refreshing the page. Crude but works for now.– redCommented Sep 19, 2011 at 7:57
I did this way
function hook_form_views_exposed_form_alter(&$form, &$form_state) {
global $user;
$account = user_load($user->uid);
$term = taxonomy_term_load($account->field_user_industry[LANGUAGE_NONE]['0']['tid']);
unset($form['field_event_industry_tid']['#size']);
if ($form['#id'] == 'views-exposed-form-events-events-pane') {
// get the view argument if defined
$term_filter=$_GET;
unset($term_filter['q']);
$term_arg = $term_filter;
if (!empty($term_arg)) {
$terms = $term_arg;
}
// change our input as required
if (isset($terms)) {
$form_state['input']['field_event_industry_tid'] = $terms['field_event_industry_tid'];
}
else{
$form_state['input']['field_event_industry_tid'] = $term->tid;
}
}
}
In my case I had to change the default value according to the query, but if it isn't, I use the value I want, this way works perfectly!
Once the filter is exposed, in the configuration options for the filter, you should see checkboxes for each of the content types in your site. The ones that you check here are the ones that are selected by default when an end user sees the view.
If you need only some of your content types to be available in the form, and only some of those pre-selected, you'll need to do something like this: Add two content type filters:
- Filter 1 = only the content types that should be available. Don't expose this filter.
- Filter 2 = the exposed filter. Select the ones here that should be pre-selected by default.
-
It's more like I have an exposed filter narrowed down to two options, but I want the second one selected by default instead of the first one.– ChaulkyCommented Apr 4, 2011 at 13:02
-
One of my suggestions above should work for you still. You may need to try my 2nd suggestion using two filters: the first to narrow down the available options, the second filter to set what is selected by default. Commented Apr 4, 2011 at 23:50
-
I'll try it out, but I don't really see where you're going with this, I may need more clarification after experimenting with your suggestions.– ChaulkyCommented Apr 5, 2011 at 14:29
-
How do I use a filter to choose which is selected by default? I already have a filter that narrows down the allowed content types, and a second that is exposed, allowing them to choose between the two allowed. But how does that help choose a default value?– ChaulkyCommented Apr 10, 2011 at 17:12
Maybe you already got the answer, but here is how I solved. If you edit the Filter in the View, there's an option called "Limit list to selected items". I checked that option, and the Default Value was reset, so now the Default Value of the filter is All (with the tag Any).
See the bottom of the post at:
If you don't select any of choices under Client Gender, and check the Optional This exposed filter is optional and will have added options to allow it not to be set. box, then will be the default choice. If you select Male item, then your default selection will Male when the view is being rendered.