0

Setting: using the search_api_views module, I have created a view with an exposed fulltext search filter. In addition, I have lots of facets for the corresponding search index in a separate block (via the search_api_facetapi module). Searching and filtering works well.

Question: When no facet item is selected and no fulltext search is used, then all indexed nodes are shown in the view as search results. How can I replace this full nodes list with a custom text (e.g., “Enter a search term or select a facet item to filter our products.”)? In other words: how can I hide the results list if and only if no search filter (facet or fulltext) is applied?

1 Answer 1

0

Here’s how I solved this:

First I implemented hook_views_post_build. Note, that I could have probably used most other views processing hooks, too, as I’m only adding custom build information.

/**
 * Implements hook_views_post_build().
 */
function MY_MODULE_views_post_build(&$view) {

  // If there is no explicit search being done, then we show a message instead
  // of all search results. We still have to run the search under the hood so
  // that we have correct search facets available. Therefore we could have also
  // used most other views processing hooks here.
  if ($view->name === 'MY_VIEW_NAME' && empty($view->exposed_input['f'])
       && empty($view->exposed_input['search'])) {
    $view->build_info['MY_MODULE_show_message'] = TRUE;
  }
}

Then I added my custom message to an overridden views-view.tpl.php using the previously defined build info variable:

<!-- … views template header here … -->
<div class="<?php print $classes; ?>">

  <?php if (isset($view->build_info['MY_MODULE_show_message'])): ?>
    <h2>My Custom Message</h2>
    <p>Enter a search term or select a facet item to filter our products.</p>
  <?php else: ?>
    <!-- … normal views content here … -->
  <?php endif; ?>

</div><?php /* class view */ ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.