2

I'm having a bit of a hard time configuring a preselected exposed filter on views.

I have a View (mysite.com/view-path) with an exposed filter as a select list with options:

Option 1
Option 2
Option 3

I want to be able to access the view with a preselected option in the exposed filter from another page (mysite.com/page) with a link like so (mysite.com/view-path?option=1)

Right now i've made a form alter on the exposed view filter with this:

if (!empty($_GET['option'])) {
   $form_state['input']['field_option_tid'] = $_GET['option'];
}

The problem is that this works, but when i try to click the select list and change the value to Option 2 for example, it will revert back to Option 1.

3

Use a custom module for this

function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  if (!isset($form_state['view']->exposed_input['<filter_field>'])) {
    // OK to set the default value
    $form_state['input']['<filter_field>'] = '<new_default_value>';
  }
}

Where filter_field is the name of the exposed filter and new_default_value is the key of the value you want as the default.

Got the idea from this article

1
  • unfortunately this is basically the same thing, still did not reach my desired result. I've added an edit to elaborate. Sep 26 '13 at 16:35
1

SOLVED IT!

Got it to work by not doing any form_alter, instead i reconstructed the URL with the argument that sends me to the page view, with exactly the field_filter ID

So, basically, when you use exposed filters on a view, it automatically changes the url to something like mysite.com/view?field_filter=1, i tried to use mysite.com/view?option=1 and then make a form alter to rewrite the $form_state[input], but this will always overwrite the user POST.

1

Working with POST values didn't work on my form. And 4life's answer was close, but in the end this is the approach that worked for me complete with pretty paths:

    function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
      $current_path = current_path();
      $path_array = explode('/', $current_path);
        if(isset($path_array[1])){
          switch ($path_array[1]) {
            case 'new_value':
              unset($form_state['input']['field_nameoffield']);
              $form_state['input']['field_nameoffield']['new_value']= 'new_value';
              break;

            case 'new_value_2':
              unset($form_state['input']['field_nameoffield']);
              $form_state['input']['field_nameoffield']['new_value_2']= 'new_value_2';
              break;
          }
        }
     }
0

As 4life specified in his answer, do this in a custom module in hook_form_FORM_ID_alter(). The only difference is that you should set the $form element's #default_value property instead of injecting the default value into $form_state['input']. By doing that, you're overwriting whatever $_POST data the user has submitted.

Example:

<?php
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  if ($form_state['view']->name == 'your_view_name') {
    if (isset($_GET['option1'])) {
      $form['filter_field']['#default_value'] = $_GET['option1'];
    }
  }
}
?>
3
  • tried this exactly how you specified, however the filter value in the select list does not change, I tried everything for the option (in the dpm($form) it shows me the new value, however the filter is not changing) Sep 27 '13 at 9:56
  • Same here ... filter does not change but $form value is updated
    – Math666
    Jan 9 '15 at 10:42
  • 1
    Modifying $form_state['input']['<filter_field>'] = '<default_value>'; did the trick for me. One thing to keep in mind too ... if you let users select multiple options for hte field you want to add the default value, you should know that you have to add an array as a default value ... meaning criting somethinng like : $form['filter_field']['#default_value'] = array(0 => 'All');
    – Math666
    Jan 9 '15 at 11:05
0

Did you try some jquery to act on the href attribute of the button to rewrite the link that gives access to the page views, something like :

$(".my-button-class").each(function() {
$(this).attr("href", "/view-path?option=1");
});
1
  • Already tried to set the filter with jQuery, unfortunately I got the same behavior as setting the $form_state[input] value. Sep 27 '13 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.