Drupal

Drupal Views and custom search features

A recent project required the use of a view to create a custom search page. The view worked as expected, with exposed filters providing the multiple search options the client required.


A recent project required the use of a view to create a custom search page. The view worked as expected, with exposed filters providing the multiple search options the client required. The problem was that the view would return results on first page load. What we wanted to see on first load was the search options and no results until after form submission. The fix was small, but made all the difference.

In the view we add an argument of type Global: Null
In the argument configuration choose "Provide default argument"
Set the argument type to "Fixed entry"
Set the Validator to "PHP code"
PHP validate code:

if (count($view->exposed_input)) {
  return TRUE;
}


The value of "$view->exposed_input" will be 0 if the form has not been submitted yet.

Once this was fixed we were getting the view's "Empty text" message on first page load. The view was set to display the empty text message if the PHP code did not validate.
After trying and failing to use the same code as above to remove the text on first load, I realized that that I was missing an important extra line. The argument mentioned above has the luxury of access to $view, but the "Empty text" does not. In the "Empty text", I placed the following:

$view = views_get_current_view();
  if (count($view->exposed_input)) {
    return TRUE;
}
?>


Set the input format to PHP code. The "Empty text" will no longer appear on first page load.

Similar posts

Get notified on new marketing insights

Be the first to know about new B2B SaaS Marketing insights to build or refine your marketing function with the tools and knowledge of today’s industry.