3

How do I redirect the user to some other page, when the view returns empty result?

9

In your views No results behavior Choose ' Global: Text area ' choose php filter and add the following code.

<?php
 drupal_goto('node/1'); //path to redirect
?>
2

Better yet do it in your views-view.tpl.php and avoid using PHP in UI like.

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

if you want to pass query to new url use this for goto

drupal_goto('your_url', array('query'=>array(
'variable_name1'=>'variable_value1',
'variable_name2'=>'variable_value2',
'variable_name3'=>'variable_value3',
...... ...
)));

or if you want to goto an anchor on page use this

drupal_goto("node", array('fragment' => $node->nid));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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