2

I'm trying to combine in a smart way Contextual filters Has taxonomy term ID with Remember the last selection feature of exposed form filters in a view.

Context

  • A taxonomy vocabulary called destinations (countries & regions)
  • A view of content tagged with destinations terms : Contextual filters : Content: Has taxonomy term ID (with depth)
  • Exposed filters such as travel duration, price...
  • Important point, Remember the last selection is ticked for all the filters
  • Ajax enabled, and the whole thing displayed as a content pane

The issue

Let's say I visit taxonomy/term/spain and filter the view by price < 700$, then go to taxonomy/term/india e.g, the view will be already filtered by price < 700$, and may return empty result, which is an unwanted behaviour.

This is because across all the taxonomy/term/% paths, the exposed form form_id remains the same views_exposed_form.

How could I get a separate form_id for each of the taxonomy/term/% exposed form, so that the last selection would be kept for each term ?

Or setup my view in order to get the expected features ?

  • 1
    I don't think the remember property is stored by form id. I think it's stored against the view and display id. – Alfred Armstrong Aug 20 '15 at 10:14
  • Thanks. Could you develop "against the view and display id" please ? – Kojo Aug 20 '15 at 10:15
  • I think you'll need to create a custom handler for the filter which stores the property differently by overriding the store_exposed_input() method. – Alfred Armstrong Aug 20 '15 at 10:16
  • This sounds exactly what I'm looking for. Is it too much asking you to post an answer with a general coding approach for create a custom handler for the filter which stores the property differently by overriding the store_exposed_input() method ? I've never done such thing I'm afraid – Kojo Aug 20 '15 at 10:19
  • It's quite a job unfortunately - looks like the settings is referenced all over the place in views, not just in the filter. Quite a lot of code, which I don't currently have time to write. Hopefully someone else will have a bright idea: probably would be best to turn off the views remember setting and create something separate. – Alfred Armstrong Aug 20 '15 at 10:29
2

Without patching the module you can't...but the View object is always available in the form state, so you can use that to check where you are. e.g.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $view = $form_state['view'];
    if ($view->name == 'foo' && $view->current_display == 'bar') {
      // ...
    }
  }
}
  • Fine, thanks. But do you think I could just change the form_id into hook_form_alter such as views_exposed_form_%term ... ? I'm afraid to face very unexpected things. I have no other idea to get it work as desired – Kojo Aug 20 '15 at 10:08
  • It won't do anything, $form_id is a variable local only to that alter function. You can accept it by reference and change it, but the calling function isn't obligated to (and won't) respect that in any way. I'm not totally clear on what you're trying to do so I don't have a better suggestion, sorry – Clive Aug 20 '15 at 10:10
  • I want to keep a remembered selection per term view basis, not per global views_exposed_form – Kojo Aug 20 '15 at 10:14
  • 1
    Ok I see, you'll probably want to follow Alfred's advice in the comments and build your own handler for something like that. You could always keep your own context in the session and use that to alter the default value that's set on the form, but I'm not sure that would permeate through to the View query from a form alter – Clive Aug 20 '15 at 10:19

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.