Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dgreenbergz on
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.
Comments
easy, just enable the core
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
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
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.
Use Template
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:
Remove "blue smurf" in template.php
I find it easiest to do this in template.php
For this example, I want to replace "blue smurf" with "asset management" :-
This results in:
Small Tweak
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.
http://CrookedNumber.com
It is much better overriding the theme_box function
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
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:
Does this work for Drupal 6
Does this work for Drupal 6 as well?
And also, is it the template.php found in themes?
Yes, and yes :) Didn't quite
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()
Thanks, this was a really helpful tip.
(It comes from theme_box().)
subscribing
subscribing
thank you, thatpatguy
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
Change this:
To this:
- Ryan
String overides plus the function to change wording
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
In the end, I'm using the function in template.php only, to keep the changes all in one place.
Thanks!
Been working on trying to change the search results title for hours. Thanks so much for this!
Inserting search terms in the message
Hi,
this worked wonders, but I have another question...How would I got about inserting the search keywords in there? I've been looking around to print $_GET["keys"] or an equivalent in order to have a personalized "Your search XXXXXXXXXXX yielded no results, etc." ?
Thanks!
They're in $form_state
Check the contents of $form_state:
- Ryan
Thanks Ryan, though I can't
Thanks Ryan, though I can't get anything except "1" printed...I went to check out the doc about it, and although it seems pretty straightforward, I can't get the values, form_id or anything from there. What am I missing?
Submit function
That stuff needs to be in a function that has access to $form_state, like the forms submit or validation functions. I would then store your words in session variables so they're easily available to other pages.
It might look something like
- Ryan
Terrific!
@ thatpatguy
Thank you for this snippet, it worked great. To get the new results on the page, I needed to clear all my caches.
Hope that small bit of information helps someone.
Did anyone else encounter any
Did anyone else encounter any issues when getting this to work?
I've pasted the following into my template.php file:
However no changes are made, even after clearing Drupals cache.
Did you replace "MYTHEME" in
Did you replace "
MYTHEME
" in the function name with the actual machine name of your theme?Sorry I should of mentioned
Sorry I should of mentioned that, yes I did.
The code is fine
There isn't anything wrong with the code but it relies on the title of the page. If you change the title to "Uh Oh! Couldn't find it!" (I've just checked your site) then it won't work. I've changed your code to work with your modified title and it works ok.
Thank you for all your comments.
This worked out for me after clearing the cache.
Doesn't work for me
I've pasted this coding above and it brings an error when refreshing my site: Notice I changed the title of MYtheme to reflect my theme's name? is that correct? I also need to change the blue smurf text..
If I change to a custom title such as Sorry, or something of that sort, how does that reflect in the php below? I put this into my theme's folder in the template.php
sorry not a php expert..
Theme Name Misspelled
Is that your exact code? Because the theme name is misspelled: "acquia_propser_box".
typo on here but still not
I did have a typo thanks for catching, but even after I fixed that, I'm getting this error
Parse error: syntax error, unexpected---
any ideas?
this is my template.php
In my acquia_prosper theme, my template.php shows this now:
Is something missing? It should just read the template file in my theme folder correct. I got the error message to go away, but now it's not displaying any changes...
If you changed your page
If you changed your page title like you mentioned a couple posts above, you'll want to change
if ($title == 'Your search yielded no results')
to have whatever your new title is between the single-quotes.Also try clearing your cache so that your system knows you're using those hooks.
so now...
thanks, I changed the title as you mentioned and got the error message to go away. but it's still not changing the blue smurf text.
I changed asset management to bronze statue, but it's still not changing the text and now I am getting an error when trying to view any page:
* warning: Cannot modify header information - headers already sent by (output started at /home/.../public_html/.../themes/acquia_prosper/template.php:14) in /home/.../public_html/test/includes/common.inc on line 148.
* warning: Cannot modify header information - headers already sent by (output started at /home/.../public_html/.../themes/acquia_prosper/template.php:14) in /home/.../public_html/.../includes/common.inc on line 148.
What does that mean? and why is it displaying> i've also cleared the cache.
The weird thing is there is no line 148
Could I just remove text altogether
This is not working, another idea would be to just remove that search result text altogether
Is that possible?
Search 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.
I think I've heard that have
I think I've heard that have the closing php tag "?>" at the end of your template.php can cause problems. Its not clear why your theme function override isn't working. Maybe paste the whole template.php into a code block here for us (or provide a link to pastebin or something).
pretty simple it is...
basically the php I have above is the only coding in my template.php that is located in my acquia_propser theme folder...???
here it is again
Its not working for D7
I tried on D7.
Seems its not working.
Is there any other way for Drupal 7 ?
Duplication
I have used this code so I can change the title and content that comes back if there are no results but it is duplicating it - the default content that sits in search.module (with blue smurf) and my edited version underneath it. The title doesn't repeat though, just the content - can anyone help?
results:
Duplication error resolved
I have figured out why this doesn't work - there is a concatenation operator on the first $content - the code should be:
Would this work on Drupal 7 as well?
Thanks! I am trying to figure out how to customize the no search results on D7 but havent found a solution that works from above examples.
looking for a Drupal 7
looking for a Drupal 7 solution as well
You can use String Overrides
You can use String Overrides to change this text in D7.
http://drupal.org/project/stringoverrides
Just copy
Just copy search-result.tpl.php and search-results.tpl.php from /modules/search into your own theme, clear cache and you are away.
search-result.tpl.php - individual results
search-results.tpl.php - the wrapper for each result with the no results text too.
I realise the question goes on to ask about the smurf part but google is ranking this highly for: How to Change "Search Yields no Result Text"
Another way
Thanks for all contributions to this. I finally used preprocess function because this doesn't alter the theme function itself, it change the text before it is passes to theme.
?>
Drupal experts in Argentina! www.tilon.com.ar
Go here for easiest solution ever...
I was trying to solve the same problem and found this...
D7
How would the D7 preprocess code look for this?
Thanks
Living life in a grande way.
D8 solution
To make this work in D8 without modifying core code or writing preproccess functions, modify your base theme's item-list--search-results.html.twig file in your /theme/ThemeName/templates/* folder (or subfolder) to add the following code to override the empty message:
For example, if you a using Classy as your base theme, your new /themes/MyTheme/templates/overrides/item-list--search-results.html.twig file would look like :
drupal 10 update for changing no results text
Thanks @diamondsea for your solution above to this problem.
Based on the code provided in 2018 by diamondsea, below is an updated version that works for Drupal 10 (running on 10.2.0).
This template needs to be placed in the templates directory for the theme one's drupal site is using.
for example in a folder much like the following:
{site base directory}/themes/custom/snazzycustomdrupaltheme/templates
where {snazzycustomdrupaltheme} would be replace with the name of your drupal site's theme.
The relative file path would be the following:
{site base directory}/themes/custom/snazzycustomdrupaltheme/templates/item-list.html.twig
Note that the template code change just above is based on the following code in the current SearchController.php code, where the array that needs modification is now named #value:
Add a block if "Search no result"
Is it possible to add a block instead of plain text if the search does not produce results? I tried entering with this:
'search # noresults': return echo views_embed_view ('view_candidature', 'block_177');
but it does not work. Can someone help me?