Posted by mdblue on January 19, 2009 at 12:04am
Jump to:
| Project: | Views |
| Version: | 7.x-3.0-rc1 |
| Component: | taxonomy data |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (won't fix) |
Issue Summary
- Created a view block with "search terms" exposed field and "taxonomy term" exposed field.
The Default value for "taxonomy term" is "Any".
View works perfectly and filters based on taxonomy term and/or search Term.
Question: Is it possible to create "Argument" to pass the url to "Default Value" for Taxonomy Term Exposed filter ?
For example, when a url is visited with taxonomy term "A", the Default Value of the Exposed Filter will be "A" instead of "Any" so that users can search within "A" immediately, instead choosing "A" one more time.
This is a common use case and it will be great if there is a way to proceed.
Thanks
Comments
#1
Sorry, you cannot currently accomplish this with Views.
#2
I would also like this to be possible. Views is keeping arguments and using them, and passing them to filters would be better.
#3
+1
#4
+1
#5
This can be accomplished like so. Hardly elegant but it does seem to cut the mustard.
The contents of a CCK select list are used rather than a taxonomy term but the problem is much the same.
View configuration
View accepts a single argument derived from a CCK field select list widget.
Filter is exposed as a block.
Filter block also includes the same CCK field as referenced in the argument.
Issue is that when filtering the view using the argument the block filter selection does not match the argument.
In the snippet below 'field_make_value_many_to_one' is the name of the form select item that you want to match to your argument.
<?php
function mymodule_form_alter(&$form, $form_state, $form_id)
{
// function seems to get called twice so need to cache
static $term;
switch($form_id) {
// issue:
// when filter view using argument the exposed filter block selection does not
// match the argument
//
// we want to modify the form select element selection state
case 'views_exposed_form':
// take a peek
// dvm($form_state['view'], 'hook_form_alter');
// get the view argument if defined
$term_arg = $form_state['view']->args[0];
if (isset($term_arg)) {
$term = $term_arg;
}
// change our input as required
if (isset($term)) {
$form_state['input']['field_make_value_many_to_one'] = $term;
}
break;
}
}
?>
#6
nice - where i have to put this snippet?
#7
You will have to put it into a module, hence the mymodule_ prefix.
Read up a bit on Drupal module hooks if you are in doubt about this.
In essence hook_form_alter simply doles out form definitions to any modules that are interested.
#8
thanks a lot
momper
#9
+
#10
subscribe
#11
subscribing. this looks good. thanks for the tip.
Chris
#12
Do you mean that's it's logically impossible? or that there is no current mechanism?
It sounds like a useful feature to me.
#13
I'm also interested in having a default value different from 'any'.
#14
Yes this can be done. I just did it....we have over 25000 mp3 files. I created a view with filters based on categories, year, month, and day.
My problem was that when the page is opened for the first time, it takes for ever to load all 25000 entries. I wanted to limit the result by providing the default year argument to latest year 2011. I tried this and worked...this is what I did...
After you expose the filter, you will notice 2 settings....
1) Operator: Is one of - It is already selected and you have to take action
2) Select terms from vocabulary [your vocabulary name]: -
In item 2 above, just select any value that you want to be a default filter, thats it. Save the view and try it. You will see that the page will be filter with value you selected.
I have 4 different filters, all of them are exposed. Now I can change the default value thru any of the filters any time.
I hope I was clear about it. Let me know if you have any questions.
Shriji
#15
subscribe
#16
Hi:
I'm posting a corrected version of hack #5 that works for Views 3 in D7. I've changed some logic to avoid warnings when there's no argument in the url and, the most important thing, I've change the hook definition to get form_state by reference. I don't know how someone could make it work without this change... I've added a check for the view name and display so this doesn't affect other exposed filters.
If you use this code you must include it in a module and change uppercase strings with your own (in my case MY_EXPOSED_FILTER_FIELD_NAME was field_section_tid)
Hope this helps some one...
/*** Implements hook_form_alter().
* issue: http://drupal.org/node/360780
* when filter view using argument the exposed filter block selection does not match the argument
* we want to modify the form select element selection state
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'views_exposed_form':
if ($form_state['view']->name=='MY_VIEW_NAME' && $form_state['view']->current_display=='MY_DISPLAY_NAME') {
//dpm($form_state, 'hook_form_alter');
// get the view argument if defined
if (isset($form_state['view']->args[0])) {
// Set form filter input value to term tid
$form_state['input']['MY_EXPOSED_FILTER_FIELD_NAME'] = $form_state['view']->args[0];
}
}
break;
}
}
Thanks to muggin for his initial code.
PD: Changing to D7 and Views 3