- 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

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Sorry, you cannot currently accomplish this with Views.

mattbk’s picture

I would also like this to be possible. Views is keeping arguments and using them, and passing them to filters would be better.

joostvdl’s picture

+1

momper’s picture

+1

mugginsoft.net’s picture

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.

 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;
  }
}
momper’s picture

nice - where i have to put this snippet?

mugginsoft.net’s picture

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.

momper’s picture

thanks a lot

momper

Marko B’s picture

+

sbydrupal’s picture

subscribe

socialnicheguru’s picture

subscribing. this looks good. thanks for the tip.

Chris

davidjmcq’s picture

Do you mean that's it's logically impossible? or that there is no current mechanism?

It sounds like a useful feature to me.

samchok’s picture

I'm also interested in having a default value different from 'any'.

shriji’s picture

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

my-family’s picture

subscribe

neurojavi’s picture

Version: 6.x-2.2 » 7.x-3.0-rc1

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

user654’s picture

.

sbydrupal’s picture

I have set the

- view name
- display name = Page
- filter field name = tid (tid is by default)

Placed the code to a module, with <?php (no end tag)

Doesn't seem to work for Drupal 7. Could you please provide more feedback on details ?

Thx

sbydrupal’s picture

Merlin, would you happen to have any input on #16 ? Thank you!

sbydrupal’s picture

Below code does not seem to work for drupal 7:

- module name: exposedfilterdefault
- view name: test
- display: Page
- field: tid

Thanks for any feedback.

How do other folks solve this problem ?

Using default taxonomy term view for the purpose of automatically capture taxonomy terms from url and use "1" view
instead of setting additional views for each category term. In this particular case, natural extension is to have exposed filter
with search terms and taxonomy filter to set to default term to search the view. How do you solve this problem otherwise ????

Thank you!

----------------------------------


<?php

/**
* 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 exposedfilterdefault_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    case 'views_exposed_form':
      if ($form_state['view']->name=='test' && $form_state['view']->current_display=='Page') {
       //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']['tid'] = $form_state['view']->args[0];
       }
      }
     break;
  }
}

TravisJohnston’s picture

Version: 7.x-3.0-rc1 » 6.x-2.16

Getting back to D6, this isn't working for me.

I've created a module for this and implemented the code from #5, changed the field_make_value_many_to_one to suit my filter which is views-exposed-form-news-block-3 for one and I will need to add another for another filter which is views-exposed-form-events-block-1.

My goal is this.

I have a News and Events page that has two blocks, News and Events. They can both be sorted depending on the Organic Group they belong to, so if you want to see what going on, you can changed the exposed Organic Groups filter.

What I did then was made it so I can have 1 single exposed filter to change the both of them. This works great.

But I have been asked to make it so if someone actually goes to a group and goes to the groups News and Events page, the same view should automatically default to the group it is in, and also allow you to change the filter to view other groups. This is easy at first with giving the view an argument to check, but of course the filter is at that point useless because once you change the filter, it doesn't meet the requirements of the argument.

To make this a little easier, I made the main news and events page a different block set since that page just needs to be set to Any by default which it is. The groups News and Events pages are a different block that needs the functionality above.

drupal12user’s picture

My solution to apply a default value to an exposed filter directly from URL:

1. I have in the URL the exposed filter value (as GET argument; e.g: I have an exposed filter on City, I go in the exposed filter in the View and click "More", you will see "Filter identifier", I put "city_filter"; then, in my URL I add "..?city_filter=something" - it can come from a menu option or a GET form etc)

2. I set Use AJAX=Yes in my View > Advanced

3. A PHP small quick fix in the "sites\all\modules\views\modules\taxonomy\views_handler_filter_term_node_tid.inc" file (because I'm using taxonomy terms): add at the end the VALUE_FORM() function the following:

if(isset($_GET[$identifier]) && isset($_POST[$identifier]) && !empty($_POST[$identifier]) && $form_state['input'][$identifier] != $_POST[$identifier]){
        $form_state['input'][$identifier] = $_POST[$identifier];
    }

This way, when the URL is called first time, the GET values are used by the exposed filters, and then if the user filters on a different value, then the POST ajax value is used (by default Views uses the GET value even if the POST one is also assigned).

Maybe this can be better integrated in the Views by somebody who works on the Views module.

Mark_’s picture

Category: feature » bug
Status: Closed (won't fix) » Needs review

#22 works great!

rdeboer’s picture

See Views Filter Harmonizer for a more elegant solution to #22 in D7.

chris matthews’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

The Drupal 6 branch is no longer supported, please check with the D6LTS project if you need further support. For more information as to why this issue was closed, please see issue #3030347: Plan to clean process issue queue