How to Change "Search Yields no Result Text"

dgreenbergz - March 23, 2007 - 11:34

Hi,

How can we change the wording of this Search related text. We want examples other than "blue smurfs". more company specific, although they are cute, smurfs...... : }

"Your search yielded no results
• Check if your spelling is correct.
• Remove quotes around phrases to match each word individually: "blue smurf" will match less than blue smurf.
• Consider loosening your query with OR: blue smurf will match less than blue OR smurf.

thanx,
Doug
FOB, Inc.

easy, just enable the core

nisguy - March 23, 2007 - 18:22

easy, just enable the core Locale module and create a new 'language'. Then, search for the text 'blue smurf' and edit the corresponding string. more info at http://drupal.org/handbook/modules/locale

Yeah enabling a translation

papile - March 23, 2007 - 19:25

Yeah enabling a translation adds a lot of overhead to just change one line, but unfortunately it is the only way it can be done without editing search.module. A noresult search calls the search_help implentation of hook help directly. There is no way to override it as it is not given a custom theme function or form.

The search module really needs the ability to be customized a lot more. For instance, I seek the ability to index only nodes of a certain type so I am not filling the search index with things that I do not need indexed. Also the ability to make your own no results field seems like a feature that should be in there as it is trivial.

Patch for Drupal 5.1 search.module

skelly - July 19, 2007 - 03:15

Here's the patch I used to easily modify the text displayed when no search results are returned. It does involve a patch to core but if you are not adverse to such things or can easily track them with source control (as I do) then it's a bit neater than trying to trick the theme into doing it for you.

Index: /public_html/modules/search/search.module
===================================================================
--- /public_html/modules/search/search.module (revision 314)
+++ /public_html/modules/search/search.module (working copy)
@@ -91,6 +91,15 @@
'\x{4e00}-\x{9fbb}\x{f900}-\x{fad9}');

/**
+ * Default text to display when no search match found
+ */
+define('SEARCH_NO_RESULT_DEFAULT', '<ul>
+  <li>Check if your spelling is correct.</li>
+  <li>Remove quotes around phrases to match each word individually: <em>"blue smurf"</em> will match less than <em>blue smurf</em>.</li>
+  <li>Consider loosening your query with <em>OR</em>: <em>blue smurf</em> will match less than <em>blue OR smurf</em>.</li>
+</ul>');
+
+/**
  * Implementation of hook_help().
  */
function search_help($section) {
@@ -103,11 +112,7 @@
     case 'admin/settings/search':
       return '<p>'. t('The search engine works by maintaining an index of the words in your site\'s content. You can adjust the settings below to tweak the indexing behaviour. Note that the search requires cron to be set up correctly.') .'</p>';
     case 'search#noresults':
-      return t('<ul>
-<li>Check if your spelling is correct.</li>
-<li>Remove quotes around phrases to match each word individually: <em>"blue smurf"</em> will match less than <em>blue smurf</em>.</li>
-<li>Consider loosening your query with <em>OR</em>: <em>blue smurf</em> will match less than <em>blue OR smurf</em>.</li>
-</ul>');
+      return variable_get('search_no_result_text', t(SEARCH_NO_RESULT_DEFAULT));
   }
}

@@ -228,6 +233,10 @@
   $form['indexing_settings']['minimum_word_size'] = array('#type' => 'textfield', '#title' => t('Minimum word length to index'), '#default_value' => variable_get('minimum_word_size', 3), '#size' => 5, '#maxlength' => 3, '#description' => t('The number of characters a word has to be to be indexed. A lower setting means better search result ranking, but also a larger database. Each search query must contain at least one keyword that is this size (or longer).'));
   $form['indexing_settings']['overlap_cjk'] = array('#type' => 'checkbox', '#title' => t('Simple CJK handling'), '#default_value' => variable_get('overlap_cjk', TRUE), '#description' => t('Whether to apply a simple Chinese/Japanese/Korean tokenizer based on overlapping sequences. Turn this off if you want to use an external preprocessor for this instead. Does not affect other languages.'));

+  // No result text:
+  $form['no_results'] = array('#type' => 'fieldset', '#title' => t('No Results Text'));
+  $form['no_results']['search_no_result_text'] = array('#type' => 'textarea', '#title' => t('No results message'), '#default_value' => t(variable_get('search_no_result_text', SEARCH_NO_RESULT_DEFAULT)), '#description' => t('Text to display when no results returned for search'));

   // Per module settings
   $form = array_merge($form, module_invoke_all('search', 'admin'));

Use Template

tate99 - September 26, 2007 - 13:32

One simple approach is to use templates. You can created a template called page-search.tpl.php. Then check if the page contains no results. If so, do some replacing. For example:

$check = '<li>Check if your spelling is correct.</li>';
if(strpos($content,$check){
  $content = str_replace('blue','physics',$content);
  $content = str_replace('smurf','journals',$content);
}

Remove "blue smurf" in template.php

DerekAhmedzai - March 25, 2008 - 11:56

I find it easiest to do this in template.php
For this example, I want to replace "blue smurf" with "asset management" :-

<?php
function _phptemplate_variables($hook, $vars = array()) {
  if (
$hook == 'page') {
   
//annihilate smurfs :)
   
if (false !== strpos($vars['content'], "blue smurf"))
      {
       
$vars['content'] = str_replace("blue smurf", "asset management", $vars['content']);
       
$vars['content'] = str_replace("blue OR smurf", "asset OR management", $vars['content']);
      }
  }
  return
$vars;
}
?>

This results in:

  • Check if your spelling is correct.
  • Remove quotes around phrases to match each word individually: "asset management" will match less than asset management.
  • Consider loosening your query with OR: asset management will match less than asset OR management.

Small Tweak

CrookedNumber - June 18, 2008 - 19:52

Thanks, Derek. Easy, simple, flexible solution. Though when I implemented it, I tweaked one line -- for efficiency.

From:

if ($hook == 'page') {

to:

if ($hook == 'page' && arg(0)=='search') {

NB: assumes that you haven't changed search's standard path.

It is much better overriding the theme_box function

danieltome - August 20, 2008 - 01:44

Instead of hacking the _phptemplate_variables function:

You can create a function in your template: MYTHEME_box($title, $content, $region = 'main')
And check if $title equals "Your search yielded no results"
And update the $content to show whatever you prefer.

This is definitly much more efficient then having the if check in the _phptemplate_variables function.

cheers,

an example of this function

thatpatguy - October 6, 2008 - 14:24

Just to help others like me who are still newbs to drupal (and potentially even newbs to php) I've added an example of what this function would look like so people can copy and paste at their desire. This function goes into the template.php file:

<?php

function MYTHEME_box($title, $content, $region = 'main') {
  if (
$title == 'Your search yielded no results')
  {
   
$content = '<ul>';
   
$content .= '<li>Check if your spelling is correct.</li>';
   
$content .= '<li>Remove quotes around phrases to match each word individually: <em>"asset management"</em> will match less than <em>asset management</em>.</li>';
   
$content .= '<li>Consider loosening your query with <em>OR</em>: <em>asset management</em> will match less than <em>asset OR management</em>.</li>';
   
$content .= '</ul>';
  }
 
$output = '<h2 class="title">'. $title .'</h2><div>'. $content .'</div>';
  return
$output;
}

?>

Does this work for Drupal 6

johlin23 - April 15, 2009 - 20:44

Does this work for Drupal 6 as well?

And also, is it the template.php found in themes?

Yes, and yes :) Didn't quite

tatude - May 12, 2009 - 12:37

Yes, and yes :)

Didn't quite figure out where the function name comes from (the _box in the end)... But I guess that's relatively easy to find out somewhere.

theme_box()

cubbtech - September 10, 2009 - 22:20

Thanks, this was a really helpful tip.

(It comes from theme_box().)

subscribing

Yuki - September 10, 2009 - 23:47

subscribing

thank you, thatpatguy

eff_shaped - November 20, 2009 - 17:14

this was just what I needed :) .... At 5pm... now I want more!

How can I add to this function to change the title also?

My bumbling php attempts have not helped.

Change this: <?php  if

rschwab - November 20, 2009 - 19:08

Change this:

<?php
 
if ($title == 'Your search yielded no results')
  {
   
$content = '<ul>';
   
$content .= '<li>Check if your spelling is correct.</li>';

?>

To this:

<?php
 
if ($title == 'Your search yielded no results')
  {
   
$title = 'My new title';
   
$content = '<ul>';
   
$content .= '<li>Check if your spelling is correct.</li>';

?>

- Ryan

String overides plus the function to change wording

eff_shaped - November 23, 2009 - 10:15

Cheers Ryan!

That worked.
(I tried something like it, but got an 'unexpected title' error with what I coded.)

For others' information:
I also worked out that I could change the title string with 'string overides' module.
In this case I had to change the title referenced in the php function to match 'My new title'; so that the function still works:

Option using string overides

<?php
 
if ($title == 'My new title')
  {
   
$content = '<ul>';
   
$content .= '<li>Check if your spelling is correct.</li>';
   
$content .= '<li>My new content.</li>';

?>

In the end, I'm using the function in template.php only, to keep the changes all in one place.

 
 

Drupal is a registered trademark of Dries Buytaert.