Here's a nice little gem i've learned today.
I needed to have one exposed filter search on multiple fields in views.
Here's one way to get this working.
Let's say we have 3 CCK text fields we want to search on: first name, last name and location.
What we need, is be able to make an 'OR statement' between the different views filters.
For this, i used the 'views OR' module.
We setup an OR statement by adding a 'views OR: Begin alternatives' filter, which you can find under the 'views or' group.

Next, add the first cck filter, expose it, and set it to 'contains' for searching.
Do this for all the other fields, but between each field, you add a 'Views Or: Next alternative' filter between them, and a 'Views Or: End alternatives' after the last filter you want to share.
So you should have something like this:

Now we need to export our view so we can make a module of it with hook_views_default_views(), have a look at this article on how to do this.
Once you have your view exported to a module, look for your your exposed filters in the code and change the 'identifier' to 'search' for example. Do this for the 3 filters.
It is important that they have the same identifier, otherwise it won't work.
'operator' => 'contains', 'value' => '', 'group' => '0', 'exposed' => TRUE, 'use_operator' => 0, 'operator' => 'field_firstname_value_op', 'identifier' => 'field_firstname_value', // <== change this value 'label' => 'Firstname', 'optional' => 1, 'remember' => 0, ),
Save your module, enable it, and flush views caches at views tools.
(admin/build/views/tools)
If everything went fine, you should now be able to share multiple exposed filters.
One more thing, we still see the other filters, or even only the filter labels.
We can unset them with another views hook, hook_form_views_exposed_form_alter().
Have a look at following example to unset the other fields.
function custommodule_form_views_exposed_form_alter(&$form, $form_state) { switch ($form_state['view']->name) { case 'customview': break; } }
Of course, your module, views and field names will be different, so don't forget to change them. ;)
Voila, one exposed filter field, searching within multiple shared fields.
Thanks to the excellent comment by jpklein, and mzenner for the tip.






Views 3
I believe this will be possible 'out-of-the-box' with Views 3. I saw a presentation of this new version at drupalcon Copenhagen a month ago and there are really a lot of small improvements which stuff like this in the new release.
Mulitple view selection
As you said that multiple filter is built in view 3. But it is possible that you have three filter with drop down selection. but one filter will populated the value on the basis on another filter. Example once you select the product category filter (drop down in views) and the another filter of product items will display only the value of selected product category value.
Hi, I was wondering whether I
Hi,
I was wondering whether I can get some guidance. I have followed all the steps, exported the views, read the Drupal Default views page, and changed the identifiers and that’s it. I’m now stuck! Do I need to create a custom module for this script??
Thanks in advance
It Works!
I just imported this in as a new view and IT WORKS!!! One question, though, where do I put the code to unset the other fields?
Thanks, so much! I've been working on this for two weeks!
Wes
unset function
I'm having trouble understanding the unset function.
custommodule - should be the name of my module, yes?
'customview' - should be the name of the Views we're working on, yes?
'filter-field_lastname_value' - should be the name of what? the filter? the CCKField it refers to?
Could you help me?
Searching fields of different types
Can we use a single search text box to search fields of different types? e.g. a title field and a date field.If not using this module, then is there any other way out
Thanks
Many thanks!! I needed this for the website I'm building. Is this still the right way to go for drupal 6.22? And what about drupal 7? Is this feature simplified in that version or do you still need to do this?
Could you please explain how
Could you please explain how all this applies for Drupal 7 and Views 3?
Also interested in doing this
Also interested in doing this in Drupal 7 and Views 3. Thanks.
No code new module solution!!!
http://drupal.org/project/views_filters_populate
Post new comment