I have found a way to initially show no results when a view loads, but if I want to display a specific value if nothing is found, that's a different story. One instance that you would want to do something like this is when you're making a distributor or representative locator. In this case, if no results are found, then you want to display the corporate office's contact information.
I am assuming you have selected the fields you want to display, filtered how you wanted, and exposed what filters you want the user to use.
The first set is to add an argument of null. After clicking on the '+' in the arguments section of the display, select Global from the group dropdown.
When configuring the argument select the following options:
- Under where it says ACTION TO TAKE IF ARGUMENT IS NOT PRESENT
- Select provide default argument.
- Provide a default argument by supplying PHP Code
- In the PHP Argument Code Box paste the following code:
$is_filtered = FALSE; foreach ($view->filter as $filter) { if ($filter->options['exposed']) { if (!empty($view->display[$view->current_display]->handler->handlers['filter'][$filter->options['id']]->value)) { $is_filtered = TRUE; break; } } } return $is_filtered;
- The Validator needs to be set as PHP Code
- Select ACTION TO TAKE IF ARGUMENT DOES NOT VALIDATE
- Make sure that there actually isn't anything in the settings for 'Empty Text'
Now the view will not display anything until you filter it. However, the next step is to only display something if there aren't any results. Throw the following code into a glue module:
function MODULENAME_views_pre_render(&$view) { if ($view->name == 'VIEWNAME' && count($view->result) == 0) { if ($view->current_display == 'DISPLAY' && $view->exposed_data['EXPOSEFIELD'] != 'All') $view->attachment_after = HTML; } }
Change the following to fit your needs:
- MODULENAME - The name of your module.
- VIEWNAME - The name of the view you are working with.
- DISPALY - This may or may not be needed. If you have more than one display on this view, then you will need to be specific.
- EXPOSEDFIELD - This will vary depending on what field you have exposed or not.
- HTML - The message, plain text or HTML to display when no results are found.