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.

Comments

Charlie Sibbach’s picture

Forgot 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:

$item['search_query'] = "My search query string";
$view->set_item($display_id, 'filter', 'keys', $item);
Charlie Sibbach’s picture

And 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.

merlinofchaos’s picture

Status: Needs review » Needs work

This 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.

kanani’s picture

Version: 6.x-3.x-dev » 6.x-2.1
Category: task » bug
Status: Active » Needs work

I 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

On a recent site I developed in Drupal 6, was faced with the challenge of implementing filtered search (search results filtered down by taxonomy terms). This can be accomplised use the default advanced search in drupal. but implementing this method would require significant rework of the search interface, not to mention additional templates required to make search results fit in with the rest of the site content which was primarily delivered via CCK/Views.
 
After some experimenting with a number of alternatives, I ended up implementing a solution that utilized an exposed search filter, and a custom search form to pass keys to the view. Unfortunately, Views 2 requires an exposed filter, so i ended up faking it by enabling the exposed filter, and then hiding it with CSS.
 
 
 
 
 
Because the View needs a get request to trigger the filter, simply adding ?keys=search-term to the url won't work. So I rewrote the Local Tasks or menu tabs using localized_options and query settings,  then rebuilding the links with the l() function
 
function hook_menu_item_link($link) {
 
/*** some code ***/     $link['localized_options']['query'] = 'keys='. $_GET['keys'];

  $newLink = l($link['title'], $link['href'], $link['localized_options']);  return $newLink;
}
 
 
The custom search form looks like this
function module_search_form(){        $form['keys'] = array(            '#type' => 'textfield',            '#size'=> 15,        );        $form['#action'] = base_path() .'search-view/results/all';        $form['submit'] = array(                        '#type' => 'submit',                '#value' => t('Search'),        );        $form['#method']= 'get';        return $form;    }

fumbling’s picture

Do 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?

fumbling’s picture

Forgive 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.

merlinofchaos’s picture

It has not. I have not had the time to rewrite the search to be more normal.

lurkerfilms’s picture

What about doing:

    $view = views_get_view('my_view_name');
    $view->set_display('page_1');
    if (isset($view->display_handler) && !empty($search)) {
        $handlers = $view->display_handler->get_handlers('filter');
        $keys_handler = $handlers['keys'];
        $keys_handler->options['expose']['identifier'] = 'keywords';
        $form_state = array('values' => array('keywords' => $search));
        $keys_handler->exposed_validate(NULL, $form_state);
    }

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 get display_handler to be set.

esmerel’s picture

Status: Needs work » Active

If 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.

iamjon’s picture

Version: 6.x-2.1 » 6.x-3.x-dev
Category: bug » task

I'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

hedac’s picture

Version: 6.x-2.1 » 6.x-3.x-dev
Category: bug » task
Status: Needs work » Active

a search terms argument would be so nice.
subscribing

rhawkins’s picture

Cross 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.

mustanggb’s picture

Status: Active » Closed (won't fix)