Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is it possible to have one exposed filter apply to multiple fields? I did search around the issues posted here, and stumbled across the views_or module. However, while I can use that to OR filters together (one for each field), it still means I have to expose one filter for each field. I guess this would work if I could hide everything except the first filter, and then have the other filters automatically get set to the value of the first.
Comment | File | Size | Author |
---|---|---|---|
#50 | views_filters_populate.tar_.gz | 1.62 KB | hanoii |
Comments
Comment #1
patchak CreditAttribution: patchak commentedThis is also something I would be interested in. For example I have 4 email fields in this site I took over, and now users of the site would like to query all those 4 email fields at the same time and not search one by one...
Is there any way to do this?
Thanks
Comment #2
susannaschroeder CreditAttribution: susannaschroeder commentedI too am very interested in this. We need to create a search form that has one form input field, and searches within multiple db fields, including a taxonomy field.
Imagine we accept a "key word" - then we need to find every record where that keyword is in the tile, and in the summary.
How can we modify a view query (safely) to do this?
Thanks,
Susanna
Comment #3
merlinofchaos CreditAttribution: merlinofchaos commentedThere's no really good way to do that in Views. Maybe with a lot of code hacking you could accomplish it.
Comment #4
jjesus CreditAttribution: jjesus commentedIs there a way to modify the SQL query used for the view to include an OR in the WHERE clause?
I have a similar requirement ... a School table has columns City/State/Country ... I have only a single input text field that I want to match against any of those 3 location fields.
Comment #5
jjesus CreditAttribution: jjesus commentedI ended up using VIEWS_OR and a custom form. http://drupal.org/project/views_or
Comment #6
rapsli CreditAttribution: rapsli commented@jjesus, could you post some code of how you did it, because I'm looking for exactely the same solution.
Comment #7
latte CreditAttribution: latte commentedIf you could post how you did this you would have some pretty good karma around here.
Seems like a bunch of people are looking for this option.
Thanks in advance.
Latte/
Comment #8
anthonyjhall CreditAttribution: anthonyjhall commentedSome relevant stuff here:
http://drupal.org/node/595108
http://stackoverflow.com/questions/697728/can-i-use-one-single-exposed-f...
Comment #9
SpriteGF CreditAttribution: SpriteGF commentedAnother thread recommended CCK Computed Fields:
http://drupal.org/node/126522
Comment #10
darrenmothersele CreditAttribution: darrenmothersele commentedI came up with a solution based on the idea given for multiple views on the stackoverflow link you gave.
Unfortunately, it relied on a 'feature' in Views 2.6 that has now been fixed as a bug in 2.8. i.e. you can no longer create multiple exposed filters that have the same filter identifier.
I'm now looking for an alternative solution...
Comment #11
dawehnerWhy do you not set different operators?
Comment #12
darrenmothersele CreditAttribution: darrenmothersele commented@dereine I'm not sure I understand how that would help?
Comment #13
dawehnerI mean different identifiers.
Comment #14
darrenmothersele CreditAttribution: darrenmothersele commentedBecause I want all exposed filters to operate on the same value - without the user having to enter exactly the same value into all of them. In 2.6 you can do this - it exposes one filter box, and the same value is used for all filters.
Comment #15
darrenmothersele CreditAttribution: darrenmothersele commentedBecause I want all exposed filters to operate on the same value - without the user having to enter exactly the same value into all of them. In 2.6 you can do this - it exposes one filter box, and the same value is used for all filters.
Comment #16
jpklein CreditAttribution: jpklein commentedJust because Views UI doesn't allow you to set the same filter identifier doesn't mean you can't still do it... For anyone that stumbles on this thread like I did, here's a recipe that worked for me.
I had three fields in my view that I wanted my users to be able to search across using a single text field: Name, Display Name, and Title.
First, install the Views_Or module and set up your view's Filters as follows:
When you're exposing these fields, set the Filter Identifier to be the same except append a different digit at the end. In my case, I used "search1", "search2", and "search3".
Next, select the tab to Export the view. Create a new custom module and paste the exported code into a hook_views_default_views() function. Search through this code for the filter identifier you set, and remove the final digit for all the fields you want to search using the same field, like this:
Even though they're all set to use the same argument from the URL, your form will still display all three filter fields unless you remove them. To do that, create another function in your custom module implementing hook_form_views_exposed_form_alter(). In this code, check for the name of the view first, then unset the $form['#info'] entries for all but the first exposed filter field -- print_r() is your friend here. In my case:
Save and enable your custom module, then make a trivial change to the view you started creating via the UI and save it. Back in the views list you should see an option to 'Revert' the view you just edited. Select that, then go to Views->Tools and clear the views cache, and you're all set. Hope it helps!
Comment #17
rickyd1 CreditAttribution: rickyd1 commentedI like your post. I wasn't in need of this, but I am going to have to try this just in case I ever have a client that needs this feature. Thanks for the post.
Comment #18
Cyberwolf CreditAttribution: Cyberwolf commentedSubscribing.
Comment #19
keitarNo CreditAttribution: keitarNo commentedThat did the trick. Thanks !
Comment #20
dawehnerSo this is fixed? If yes make the issue as fixed
Comment #21
robby.smith CreditAttribution: robby.smith commentedHi!
I was wondering if there is a chance of this becoming a feature request? (any major benefits other than this use case?) Or should it remain as a modification done by a custom module?
When a Filter Identifer that already exists is entered:
Current message: | This identifier is used by another handler. |
Suggested message: | This identifier is used by another handler. use it anyway |
- An override that would allow admin to force the Filter Identifer after a warning message.
Just a thought I had while trying #16 which worked nicely!
Thanks
Comment #22
darrenmothersele CreditAttribution: darrenmothersele commented+1 for this being a warning, rather than a validation error.
Comment #23
JThan CreditAttribution: JThan commentedThx to #16. Just add that hook_views_default_views needs to return an array containing the view
Then this is ready to go.
Comment #24
30equals CreditAttribution: 30equals commentedi'm trying to do the same thing, and followed the instruction of #16, but the code has no effect at all...?
Comment #25
danielbeeke2 CreditAttribution: danielbeeke2 commentedHey,
if you want to do like #16 but with less coding you can hack into core and comment out these lines in views_handler_filter.inc
if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
}
Daniel Beeke, At the Rise of Day
Comment #26
simeThis can be done with computed fields module, particularly if all the fields you want to filter are from one node type.
http://drupal.org/node/126522
1) Create a new computed field (maybe call it 'field_search_data') on the target node type.
2) Configured it so that it aggregates all the other fields you want to filter on and saved the result in it's own db field.
3) In the fields display, hide the output of the field
4) In the view add a filter for the new field.
I think this is quite a nice solution that I've used a few times. So based on @merlinofchaos' opinion that an equivalent feature won't be built into views, I'm going to close the issue.
Comment #28
khan2ims CreditAttribution: khan2ims commentedHi,
I followed #16 and also took into consideration #23. Now it displays only one field, but doesn't search the fields which have been commented out. I am using View 3 on Drupal 6. Do I need to change anything else?
Edit: I don't see "revert" link for the view, on the views list
Comment #29
nitin.xiitec CreditAttribution: nitin.xiitec commentedSomething like this maybe an easier option. Use drupal_set_message(print_r($query)) to find your array variables. You will need the view_or module to first set the variables and then just put these functions in your module.
Thanks to #16 for the start but that method doesn't seem to work. My module doesn't call the first function. Therefore I went ahead with this shorter method.
<?php
function custom_search_views_query_alter(&$view, &$query) {
if ($view->name == 'SearchJobs') {
$query->where[1]['clauses'][1]="(node_revisions.body) LIKE ('%%%s%%')";
$query->where[1]['args'][1]=$query->where[1]['args'][0];
}
}
function custom_search_form_views_exposed_form_alter(&$form, $form_state) {
switch ($form_state['view']->name) {
case 'SearchJobs':
unset($form['#info']['filter-title']['label']);
unset($form['#info']['filter-body']['label']);
unset($form['body']);
$form['submit']['#value'] = t('Search for Jobs');
break;
}
}
Comment #30
betz CreditAttribution: betz commentedThanks jpklein, that worked smoothly!
Made an article of this trick on
http://krimson.be/articles/2010/09/25/views-searching-multiple-fields-on...
Comment #31
jrz CreditAttribution: jrz commentedThanks BEtz for your article.
Comment #32
kmajzlik CreditAttribution: kmajzlik commentedany solution for 6.x-3.x ?
Comment #33
ziomizar CreditAttribution: ziomizar commented#26 is the simplest solution for me.
Works like a charm and not require creation of new module.
Only problem of this solution, maybe that need a lot of space for storing a big amount of data.
Comment #34
John Carbone CreditAttribution: John Carbone commentedI needed this same functionality. Using #16 as a starting point, I did my filter identifier changes programmatically vs. in an exported View since we like to export Views regularly, I didn't want to create a maintenance issue. This was used to create a single form field searching across a custom Location field and the node title. Here's how I got there. It picks up at "Next, select the tab to Export the view." from #16 above.
Comment #35
Anonymous (not verified) CreditAttribution: Anonymous commented#16 and #23
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?
Comment #36
TYPEX CreditAttribution: TYPEX commentedNice solution if you are using a node view. This solution along with one suggesting the use of the computed fields module both require modules that work w nodes. Does anyone have a solution for combining exposed views in user profile fields? Muchas gracias
Comment #37
TechNikh CreditAttribution: TechNikh commentedfor views3 unset($form['#info']['filter-field_name']; is not sufficient. it's showing the extra textboxes near the apply button. Any idea how to fix this?
Comment #38
TechNikh CreditAttribution: TechNikh commentedI take my previous comment back. It's working perfectly for views3
Comment #39
nourcy CreditAttribution: nourcy commentedI use
I had a submit and I hide fields that I doesnt need in form_alter
And I set my keywords values to others in my submit
A little dirty but its works !
Comment #40
WildKitten CreditAttribution: WildKitten commentedI tried #16 but I couldn't get it work. I don't like hacking the core, but #25 worked great!
Comment #41
afeijoHere is my code for views 7-3, for 1 exposed filter to be used for filter for node title or one taxonomy
HTH
Comment #42
doublejosh CreditAttribution: doublejosh commentedTrying to decide on my approach. This always means make a big list :)
Comment #43
hanoiisubs
Comment #44
hanoiiI will soon upload a module that would really sort this out without any hacking, basically it follows the views_or approach but it adds one new filter that can be used to populate other filter values. So in this case, you would just do the normal views_or approach except without really exposing those filters, instead, you would expose the new filter added by my module which will populate the other filters.
I might just upload it to d.o. as I feel it's a nice module, but I wonder if there are any souls on this issue to try it out. If so,. I might upload it here first.
Comment #45
hanoiiComment #46
hanoiiI was smartly pointed to Views hack, which sounds like a good place to add this feature rather than contributing a new module myself, what do you think?
Basically what this new module I did does, is exports a new filter within a 'Global' section that allows you to enter a value (via a textfield) and choose what filters of the current display should use that value (via checkboxes). This is specially useful for use cases like this one, in which you might one one exposed filter to sort them all.
Any thoughts of this being added to Views hacks? Do you think worth doing a new module? Would you give me maintainer access or you would maintain it yourself?
Thanks,
Comment #47
hanoiiComment #48
daemonsy CreditAttribution: daemonsy commentedSubscribe
Comment #49
doublejosh CreditAttribution: doublejosh commentedLove this @hanoii
Need any testing? Got a sandbox?
Comment #50
hanoiiI thought I uploaded it here, but I haven't. Attached is the module.
If this is needed I don't mind uploading contributing it as a separate module.
You might need to rename it back to tar.gz for it to work.
To use, add a new filter of the global section, called filter populate or something, expand it and select the filters that this should populate. Do not expand those filters you selected, and do the views_or combination you need as you would do without this.
Comment #51
doublejosh CreditAttribution: doublejosh commentedDid this end up landing anywhere else?
Also, there don't appear to be Global filters in Views 6.2.12
Do I need 6.x-3.0-alpha3?
Comment #52
doublejosh CreditAttribution: doublejosh commentedMy fault. Hadn't enabled it. Sorry!!! Looks wonderful now.
Comment #53
doublejosh CreditAttribution: doublejosh commentedSeems to be working for me as expected. Amazing. Saved me at least a day.
Not 100% this is from this module but, when there are no results it shows everything, much like an argument setting what to do when doesn't validate.
Comment #54
infojunkieThanks for the submission, useful handler indeed.
I found a problem trying to send values to a Node: Type and Node: Title simultaneously. I get a warning:
This is because the filter value assignment in
views_filters_populate_handler_filter::pre_query()
says:whereas
views_handler_filter_in_operator
probably expects an array of values.This raises the question of the general applicability of this module: can it only work with just string filter values?
Comment #55
hanoiiwell, at first yes, I guess there might be ways to try to improve the module and start handling more use cases, but this has one basic aim which is to try to create a text field to filter different elements, and yes, those are most likely free text. For typed filters, not sure how used that would be or how, but if necessary I can try to take a look and see if I can come up with a solution to that, for as a starter, I thin it's a pretty useful module.
Also let me know if you prefer form e to create a separate module to load off some support for that.
Comment #56
infojunkieBecause I have very little free time to take on new sub-modules, I think it would be best for you to create a new module. Thanks!
Comment #57
hanoiiI am restoring this issue name and project, and changing it's status just for leaving the issue with a proper referecne.
I have contributed the module to http://drupal.org/project/views_filters_populate, it should have a dev release soon if I haven't missed anything, feel free to try, test, contribute and report issues with that module.
Comment #58
ankitchauhan CreditAttribution: ankitchauhan commentedHi Nitin
Thank you very much for a very short and helpful method. It works fine.
I went with #16 method. But after installing views_or module and configured field as well, I was facing problem after exporting view it wasn't working somehow. I have applied your code. Now my problem is resolved.
Thanks once again.
Comment #59
Max_Headroom CreditAttribution: Max_Headroom commentedhttp://drupal.org/project/views_filters_populate
Comment #60
doublejosh CreditAttribution: doublejosh commentedSeconded. I use that and it's great.
Comment #61
a.milkovsky#59
Great module. But there is 1 problem "only populates STRING exposed filters"
Comment #62
gokulnk CreditAttribution: gokulnk commentedOn my Drupal instance I have Views 7.x-3.7 there is a filter
Global: Combine fields filter (exposed)
This lets me add multiple fields and thenenables users to search on selected fields
using a single filter.Comment #63
argiepiano CreditAttribution: argiepiano commentedConfirming #62:
Global: combine fields filter
works like a charm. More information here: https://swsblog.stanford.edu/blog/views-exposed-filters-multiple-field-searchComment #64
Ajay Gadhavana CreditAttribution: Ajay Gadhavana commentedIts awesome features by views module suppose you want to search address based on zip code or using any text of that address then you can go with global filter.
steps : FILTER CRITERIA = > ADD => Global => Global: Combine fields filter => select your fields thats it.
Enjoy :D