Additional exposed filter block for views

Views are great for searches if you configure them with exposed filters. Especially if the filters are displayed in a separate block, for example in the sidebar, so that they are accessible on different pages and, once submitted, point to a results page. Modules like Better Exposed Filters can help you create great user experiences for accessing all the content you want to be found on your website. Something that I have often missed though, is the ability to have multiple exposed filter blocks, e.g. one in the sidebar, with a subset of filters, and then another one directly above the search results that provides additional (secondary or less important) filters and / or options that control the display of the search results (things like the number of items per page or the sort options). All that good stuff that is already built into views can only be displayed in one block. I know I'm not the only one missing this.
When I came across this need again for a current project of mine (unfortunately not yet online) I decided to give it a try and to solve this problem in way of a module.

While researching I have found several possible solutions to this and you can certainly find more on the web. Some examples:

The different approaches boil down to this:

  • Clone a view with exposed filters in a block so that you have essentially two identical filter blocks that you can display independently, then hide the undesired filters using CSS.
  • form_alter your way through the exposed block and add markup, so that you can style them in a way that makes them appear to be displayed independently.

Both approaches require a certain amount of maintenance overhead and are in their essence hacky solutions for a use case that the Views module does not address. Now I can't really claim that my solution is less hacky. But at least it is configurable using the Views UI and it spares you of having several identical views just for the sake of different filter blocks (resulting in less maintenance overhead) and the work of manual altering your views forms and coping with possible side effects (resulting in more flexibility and removing the need for programming skills).

You can find the solution I came up with as a sandbox module hosted on drupal.org: MEFIBS. It approaches the problem in a different way than described above. It comes as a views plugin that provides a UI that let's you enable an additional block for the exposable elements of a view (filters, sort options, items per page). Then, for each exposed element you can decide on the options form for that element whether it should be displayed in the default block or in the additional one.

The logic is pretty simple: Behind the scenes, the original exposed form is first altered to hide all elements (using CSS's display: none) that should appear in the additional form. In a second step, the original form is entirely cloned to provide the additional form block and all elements that already appear in the original form are hidden, leaving only the elements visible that have previously been hidden in the original form. All form ids in the additional block are altered to make sure that ids are unique. On form submit the submitted values are altered before the view receives its arguments, meaning the form values are rewritten to the original form ids so that the view receives exactly the form elements it expects. As often, the basic approach is pretty simple, but problems arise when trying to make this approach working in different use cases (ajax vs. non-ajax for example).

Originally I wanted to be able to provide multiple additional blocks, but I soon realized that a stable version with a single additional block is already enough work. And having two different form blocks is solving my normal use cases and should cover a great deal of the use cases of people who have already found other work arounds for this topic. So for the moment I'm focusing on getting the current two-block solution stable. Once that is done, I might turn it into a full module and maybe add support for more blocks, but that is still a long road ...

No code samples in this post, but all the code is available on drupalcode.org.

Update: The module is now available as a full project on drupal.org: https://drupal.org/project/mefibs. It's currently in dev and I started working on multiple block support and a better UI.

Category: