3

e.g. A view for job postings has an exposed filter to search by title.

  1. I want to show a "No results behavior" if there aren't any jobs at all "There are no postings at this time. Please check back later."

  2. I also want to show different text if their search returned no results, but there are still postings available. It's just their search didn't match anything. "Sorry, there are no results matching your search criteria."

It appears that I can only do one or the other.

2
  • are you using D6 or D7 ?
    – Bala
    Sep 1, 2013 at 16:30
  • I am using Drupal 7 Sep 1, 2013 at 16:47

2 Answers 2

4

You could easily adapt the views-view.tpl.php to your specific view, eg, views-view--VIEWNAME.tpl.php and change up the portion dealing with no results...

<?php if ($rows): ?>
  <div class="view-content">
    <?php print $rows; ?>
  </div>
<?php elseif ($empty): ?>
  <div class="view-empty">
    <?php print $empty; ?>
  </div>
<?php endif; ?>

...with something along the lines of...

<?php if ($rows): ?>
  <div class="view-content">
    <?php print $rows; ?>
  </div>
<?php elseif ($empty): ?>
  <div class="view-empty">
    <?php if (count($_GET)>1): ?>
      Sorry, there are no results matching your search criteria.  Perhaps with other filters you may find some.
    <?php else: ?>
      There are no postings at this time. Please check back later.
    <?php endif; ?>
  </div>
<?php endif; ?>

...with just some really simple logic based upon parameters Views uses for the exposed filters (eg, if more than ?q=PATH is sent it will have more than just 1 in $_GET).

Now if you want to get fancier than this you may want to investigate one of the Views preprocess hooks and actually change up the $empty variable and not put a lot of logic in the template.

If that's the case you could go further and re-run the View via views_get_view_result() without any filters and if it had any results, actually return your desired Sorry, there are no results matching your search criteria. (You could do this in this template file but I really try to keep lots of deep logic outside of them)

(Is there any way to dynamically add a CSS class selector to a view with submitted exposed filters? has some more code you might be able to re-use here as well)

0

Snippet code to display different "No results behaviour" based on exposed filter results and total results:

/**
 * Implements hook_preprocess_views_view__HOOK() for job posting view.
 */
function nkf_preprocess_views_view__job_posting(&$variables) {
  $view = $variables['view'];
  if ($view->current_display == 'page_1') {
    $result_count = $view->total_rows;
    $exposedInput = $view->getExposedInput();
    // Set the no result behaviour if job_type filter selected.
    if (!empty($exposedInput['job_type']) && $result_count == null) {
      $variables['empty'] = t('There are no postings at this time. Please check back later.');
    }
    elseif ($result_count == null) {
      // Set the no result behaviour if job_type filter not selected.
      $variables['empty'] = t('Sorry, there are no results matching your search criteria.');
    }
  }
}  

Note: job_posting denotes the machine name of view, nkf denotes either module or theme name depends upon working in .module/.theme file

Informative tips: The solution of this problem to use: hook_preprocess_views_view__HOOK() instead of using hook_form_views_exposed_form_alter()

Earlier I was trying to use hook_form_views_exposed_form_alter however I wasn't able to update the no result behaviour then came to know major basic difference between them and reach to solution.

hook_preprocess_views_view__HOOK: This hook 'll be use to alter the view variables (for eg: no result behaviour variable) before rendering view.

hook_form_views_exposed_form_alter: This hook 'll be use to alter form definition/elements (for eg: alter exposed filter) before executing view.

Your Answer

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

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