1
votes

I have a view that returns search results via the search API. It performs this use case adequately and I am happy. To crown the deliverable, I need to add a title override of the form Showing search results for '%1' which looks easy enough initially but it isn't working entirely as planned.

For a URL = mysite.com/search/all?search=wombat, where the search value is gathered from an exposed form within a block, I am either getting:

Showing search results for 'Search for "all"'

or, if I enter %1 in the title override for subject not appearing in the URL, I get:

Showing search results for %1". My goal is to get "Showing search results for 'wombat'

The title override works in that it removes the Search for ... part but the substitution picks up on "all" as the exception value (or anything else that I set as the exception value) where I need to be able to pick up the value of the query string (search=wombat).

Can anyone shed some light here?

2

2 Answers

1
votes

The problem is that the '%1' and '%2' that you can use to override the title refer to your path's first and second arguments (in Drupal terms) and that would be 'search' and 'all?search=wombat' in your case...

What you need instead is the 'wombat' as a path component in itself.

Perhaps you can achieve that by working that case you're talking about: the case of a "title override for subject not appearing in the URL". There is an option in the contextual filters section (I'm assuming that's where you're working) for providing a default value when one isn't present. Perhaps you can use the 'PHP code' option there, isolate your 'wombat' string and return that as a default contextual filter, and then you can get to it via the '%1'.

The php code to get that portion of the URL should look something like this:

return htmlentities($_GET['search']);

the $_GET() returns the value of that variable in the url, and the htmlentities() is just to keep it safe, since it's using a portion of the url, which is vulnerable to XSS.

See if that combo (1) setting a default argument when one isn't present and 2) using that newly set argument in your title printout) works!

0
votes

I fixed this issue. Using following two hooks we can change the defalut value of filter Programmatically.

<?php
/**
* hook_views_pre_view
* @param type $view
* @param type $display_id
* @param type $args
*/
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'VIEW_NAME') {
    $filters = $view->display_handler->get_option('filters');   
    $view->display_handler->override_option('filters', $filters);
  }
}



/**
* hook__views_pre_build
* @param type $view
* @return type
*/
function MODULE_NAME_views_pre_build($view) {
  if ($view->name=='VIEW_NAME') {   
    $view->display['page']->handler->handlers['filter']['filter_field']->value['value'] = 8;
    return $view;
  }    
}
?>

This code worked for me. I am using the drupal 7.