Metal Toad has been building applications and cloud environments for some of the most well-known global brands for over a decade. Learn more > >

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

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Ready to get started?