
I was trying to build a view dynamically that would display in a block on the ordinary search results page, to display the results of the same search but filtered with other filters. I'm using the Search: Search Terms filter to do this (search terms not available as argument). So that I could change the search string, I built the view dynamically using the best (mostly undocumented so far) procedure I could find (a few hours of Google):
$view = views_get_view('sponsored_search');
$display_id = 'default';
$view->set_display($display_id);
$item = $view->get_item($display_id, 'filter', 'keys'); // Get the keys filter
// Here's where it's broken. This code was taken directly from views_handler_filter_search::exposed_validate
$item['search_query'] = search_parse_query(arg(2));
$item['search_query'][0] = str_replace('d.', 'search_dataset.', $item['search_query'][0]);
$item['search_query'][2] = str_replace('i.', 'search_index.', $item['search_query'][2]);
$view->set_item($display_id, 'filter', 'keys', $item);
The choice of $item['search_query'] is at this point my own decision. Spending a few hours with the code and dpm, I found that set_item puts my item into the 'options' field of the filter handler. Looking at the handler, it uses the search_query field, NOT the options['anything'] field to generate the query. So, I looked for where search_query is filled in class views_handler_filter_search. It's never put in place by anything except the exposed_validate function, which means that the only way (that I can tell) that it will ever build a real search query is if you give it a search string through the exposed filter. Whew!
Now, there is a very large chance that views is doing something magical that I don't understand, but I've looked through get and set_item() and I don't see a naming scheme in there so that this handler would work, especially looking at the code of the handler. If I am wrong, please forgive me.
But if I'm right, I have a patch here that will at least make MY application possible. I really think that search terms should properly be exposed as an argument type as well as a filter, but I don't have the wherewithal to do that myself.
Comment | File | Size | Author |
---|---|---|---|
views_handler_filter_search.inc_.patch | 821 bytes | Charlie Sibbach |
Comments
Comment #1
Charlie Sibbach CreditAttribution: Charlie Sibbach commentedForgot to mention that I moved the search_parse_query() and related code into the handler itself, so to properly set up the item option, you just need to do:
Comment #2
Charlie Sibbach CreditAttribution: Charlie Sibbach commentedAnd in a final stroke from a little hint, I find that this whole method is really the result of Drupal 5 thought patterns. I can do what I need to do with Attached Views, that share the same exposed filters, and just override the default search page with a view. Which means I don't need to programatically define a set of search terms.
However, I still think there aught to be a way to at least enter a static set of search terms without an exposed filter. Say I want to show a given set of search results in a view; that would be useful.
Comment #3
merlinofchaos CreditAttribution: merlinofchaos commentedThis has been on my list of things to fix. However, it needs to be fixed to work more like normal filters. I'm not quite sure why but the entire search filter seems to be specialcased when it should not be.
Comment #4
kanani CreditAttribution: kanani commentedI had a similiar issue that I solved by exposing the search filter, hiding it with css, and then using a custom search form to submit (using the get method) search keys to the view url. In my instance I had local tasks on the view for different taxonomies, so in template.php their is some code that rewrites the local tasks to add the query w/ the search keys.
http://www.hazelconsulting.com/blog/views-2-search-exposed-filter
Comment #5
fumbling CreditAttribution: fumbling commentedDo you still have that documentation? The following link doesn't work for me. In any event, I'm trying to create a block to display on all nodes of a certain content type that will take the title of the node, treat it as a search and display search results (in the form of a views-created block) accordingly. Would I use a similar methodology as what you've used above?
Comment #6
fumbling CreditAttribution: fumbling commentedForgive me if this is old news, but has this been fixed? I'm trying to use the Search: search terms filter, but I'm not sure how it is supposed to work without the counterpart box to enter terms into. I'd like to use this view without its views-filter box to override the standard search results page if possible. In other words, I'd like users to be able to search the site normally through the standard site-wide search box, but to see results displayed in this view.
Also, do you know if "Search: search terms" will be listed in arguments any time soon, or if there's a simple modification I could try to make that work?
Thanks for any help.
Comment #7
merlinofchaos CreditAttribution: merlinofchaos commentedIt has not. I have not had the time to rewrite the search to be more normal.
Comment #8
lurkerfilms CreditAttribution: lurkerfilms commentedWhat about doing:
where
$search
is your search string. The interfaces you are using are too low level and return only arrays and not objects e.g. the handler itself. Doing this method eliminates the need for a patch.Please note you need to call
set_display()
to getdisplay_handler
to be set.Comment #9
esmerel CreditAttribution: esmerel commentedIf someone wants to take another stab at this against the current version, feel free. Until then, I'm putting this in postponed until such time as we close it for ancientness.
Comment #10
iamjon CreditAttribution: iamjon commentedI'm marking this as an unassigned task. If anyone would like to role up their sleeves and take it upon themselves to write a patch it would be awesome
Comment #11
hedac CreditAttribution: hedac commenteda search terms argument would be so nice.
subscribing
Comment #12
rhawkins CreditAttribution: rhawkins commentedCross posting here as I think the solution is relevant.
http://drupal.org/node/600242#comment-4038736
I used the solution above and it worked great. Looks like that patch has been applied to Views 3. For views 2, you can use the patch as a module.
Comment #13
mustanggb CreditAttribution: mustanggb commented