Saturday, 25 September 2010

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.

 add filters

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:

 filters

  1. Views Or: Begin alternatives =
  2. Content: Firstname exposed
  3. Views Or: Next alternative =
  4. Content: Lastname exposed
  5. Views Or: Next alternative =
  6. Content: Location exposed
  7. Views Or: End alternatives =

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.

  1. 'field_firstname_value' => array(
  2. 'operator' => 'contains',
  3. 'value' => '',
  4. 'group' => '0',
  5. 'exposed' => TRUE,
  6. 'expose' => array(
  7. 'use_operator' => 0,
  8. 'operator' => 'field_firstname_value_op',
  9. 'identifier' => 'field_firstname_value', // <== change this value
  10. 'label' => 'Firstname',
  11. 'optional' => 1,
  12. 'remember' => 0,
  13. ),

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.

  1. function custommodule_form_views_exposed_form_alter(&$form, $form_state) {
  2. switch ($form_state['view']->name) {
  3. case 'customview':
  4. unset($form['#info']['filter-field_lastname_value']);
  5. unset($form['#info']['filter-field_location_value']);
  6. $form['submit']['#value'] = t('Search');
  7. break;
  8. }
  9. }

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.

Written byTom Behets

Coming from the world of Sound, his interest for Drupal drove him to drop the 'engineering' for 'development' and he is now known as Tom, Drupal Developer next to being a fresh Linux fanatic. He can also always be found on IRC. In the most sinister days of his past he whored himself out to the dark side as a mercenary of spam... watch those inboxes! Plans on becoming an astronaut.

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?

  1. function custommodule_form_views_exposed_form_alter(&$form, $form_state) {
  2. switch ($form_state['view']->name) {
  3. case 'customview':
  4. unset($form['#info']['filter-field_lastname_value']);
  5. unset($form['#info']['filter-field_location_value']);
  6. $form['submit']['#value'] = t('Search');
  7. break;
  8. }
  9. }

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!!!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <h4> <br>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.