I have a client that wants to show "sponsored" content in a block on top of the ordinary search results. Sponsored is a CCK field in all of my node types. I'd like to figure out the easiest way to do this. Basically, I need to create a search query based off the base search query (block will show up on search/*, so I can use arg(3) to get that, I think), with a sponsored content only caveat in it.

I was hoping I could do it with a view, but there's no argument or filter that will do the trick. What is the best way to do this?

Comments

Charlie Sibbach’s picture

So, I looked and asked around, and turns out that there IS a filter for search terms available. I'd like to embed this view into the search page without exposing the filter (it should just match the dominant search terms). How can I dynamically update the filter to do this?

Charlie Sibbach’s picture

I just spent the last 3 hours trying to make a view with a "search term" filter work WITHOUT exposing the search term. Here's what I came up with:

  $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);

Here's the trick. The search handler class has a bug in it- reading through it, the only way it gets the search_query variable is through exposed_validate! My code above works fine- if by fine I want to insert the query into the Options array, rather than in the core of the handler class! The views_handler_filter_search class does not handle options correctly, it's set up to ONLY be used while exposed!

dawnsong’s picture

Thanks. I am not very clear where should I put the codes above to?

Charlie Sibbach’s picture

Here's a patch for anybody interested, so that you can use a non-exposed Search Terms filter:

In views/modules/search/views_handler_filter_search.inc, change:

function query() {
    if (!isset($this->search_query) || empty($this->search_query[3])) {

To:

function query() {
  	// This has been added to support filter options
  	if (isset($this->options['search_query'])) {
  		$this->search_query = search_parse_query($this->options['search_query']);
  		$this->search_query[0] = str_replace('d.', 'search_dataset.', $this->search_query[0]);
      $this->search_query[2] = str_replace('i.', 'search_index.', $this->search_query[2]);
  	}
    if (!isset($this->search_query) || empty($this->search_query[3])) {

To use it, use the code I have listed above when creating your view. Its no good for just standard views as is, you need to build the view by hand as above.

fumbling’s picture

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?

doublejosh’s picture

Looking to do this too. Still haven't found a solution... other than SOLR - Views 3 kinda things.