2

I would like to redirect the "No result found" page of the drupal search to Add node. Basically I want when users search for an item using the search text box they get redirected to the Add node page when the item is not found.

1

I was really hoping that _preprocess_search_results(&$vars) would work, but it only gets called when there are results, so, here are two really really I can't believe I am suggesting these but they should work ways:

Drupal 6:

In your theme's template.php file, you can retheme box, which is how drupal 6 creates the 'no results found' info:

function YOURTHEMENAME_box($title, $content, $region = 'main') {

  if ($title==t('Your search yielded no results')) { 
    drupal_goto('node/add'); 
  } else {
    $output = '<h2 class="title">'. $title .'</h2><div>'. $content .'</div>';
    return $output;      
  }
}

If Drupal 7:

Copy search-results.tpl.php from the search module's directory to your theme directory and change it:

<?php if ($search_results): ?>
  <h2><?php print t('Search results');?></h2>
  <ol class="search-results <?php print $module; ?>-results">
    <?php print $search_results; ?>
  </ol>
  <?php print $pager; ?>
<?php else: ?>
  <?php drupal_goto('node/add'); ?>
<?php endif; ?>

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.