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 ardarvin on
I'm wondering if anyone has customized their search block form in Drupal 6.0 yet, and if so...could they provide sample code of what they did.
I know about this article: http://drupal.org/node/154137
...but I'm not sure if it'll work for drupal 6.0
Comments
Nope...the code from the
Nope...the code from the aforementioned doc didn't work in Drupal 6.
Anyone?
take a look at a d6 theme already doing it..
there are several themes for d6 with search built into header (for example) - i think 'internet center' (or something like that) has it in upper right...but i know that several do it - perhaps visit their code and see what they're calling. my guess is that the field name was changed for d6 and a simple edit will fix it...
I've found a couple themes
I've found a couple themes that use CSS to change the look of the search block, but nobody who is actually changing the search template.
I did some research and came up with this:
Make a search-block-form.tpl.php file in the theme directory, based off the one found in the drupal core. In the comments of the core file they state:
* Available variables:
* - $search_form: The complete search form ready for print.
* - $search: Array of keyed search elements. Can be used to print each form
* element separately.
*
* Default keys within $search:
* - $search['search_block_form']: Text input area wrapped in a div.
* - $search['submit']: Form submit button.
* - $search['hidden']: Hidden form elements. Used to validate forms when submitted.
The, in the custom theme template.php, I make the following function:
function mytheme_preprocess_search_block_form(&$variables) {
$variables['search']['some key'] = "some value";
....
}
Unfortunately I have no idea what all the $search[] keys are, or what their values can/should be.
How do I go about seeing the contents of this variable, or any drupal variables in general?
Again, in the search-block-form.tpl.php core file it states:
* To check for all available data within $search, use the code below.
*
*
print '<pre>'. check_plain(print_r($search, 1)) .'</pre>';
Only I'm not sure where to put this code to display the data.
Sorry, I'm new to drupal / php...just trying to figure this out, and ultimately change the "submit" button to a custom icon.
How I changed the Search Box in Drupal 6
ardarvin - you are on the right track.
Here's what I did:
1. copy search-theme-form.tpl.php from the core into my theme directory
2. Ran this (see notes in search-theme-form.tpl.php) to see what was available:
3. In theme copy of search-theme-form.tpl.php, removed
and replaced it with
Notes:
1. At first glance, it looks easier to just hardcode the changes to $search["submit"] and $search["search_theme_form"] but each instance of the search form gets a unique ID - naturally. More than one instance usually occur after the first search.
2. Obviously, you need to put in the correct image src to find your search image
3. I have decades of programming experience, but little in Drupal/PHP so this fix may not be Druapesque.
Kayaker
________________________________________________________________
Aweigh - not all who wander are lost.
http://www.aweigh.com
Yup, that code works.
Yup, that code works. Thanks.
I've added default text to the search field:
$search["search_theme_form"] = str_replace('value=""', 'value="Search mysite.com"', $search["search_theme_form"]);
However I'd like to add some javascript to get the text to disappear when a user clicks on it, as explained at: http://www.yourhtmlsource.com/forms/clearingdefaulttext.html
I've added the *.js files (as provided from the above link) into my theme directory, with a slight mod so the script searches for the "form-text" class, as opposed to the "cleardefault" class.
I've added the lines:
To the head of the page.tpl.php page.
However, I can't get it to work. Any suggestions?
btw, did you see the safari search module for d6?
it puts rounded corners on the search box according the to =search tag (changes it) works in safari, IE, ff, etc...
So I got the javascript to
So I got the javascript to work...I just needed to absolutely link to the files in the head.
ie:
Using str_replace to alter
Using str_replace to alter the vars seems to be setting things up for a failure if Drupal should ever change the default values of things.
Does anyone know how to hard code the vars, i.e. what their full values should be?
$vars['search']['search_block_form'] = ???
$vars['search']['submit'] = ???
Modifying Forms
I find a great article about modifiying forms:
http://www.lullabot.com/articles/modifying-forms-5-and-6
I updated the code you had
I updated the code you had (which works great, THANK YOU!) with a changed line to simply replace the "Search" string on the submit button to just "Go!" below.. And I left the image replacement technique code you have in there just commented out so that people in the future can pick and choose either method easily. This template will definitely be going in my Drupal theming code snippets...
-=- christopher
I too am a Drupal newb, but
I too am a Drupal newb, but no stranger to programming. Before beginning, I read almost all of the theming documentation on the site. After reading countless worthless responses about going to this page (http://drupal.org/node/173880) to find the solution, I had almost given up on trying to figure out the "Drupal way" to change the "Search this site:" (and also the "Read more") default text. I found the point of origination for the text easily enough (TextMate -> Find in Project), but figuring out how to override the functions that house the text was beyond me. It seemed like the Drupal way of doing things would necessitate copying the original function into my theme's template.php file, then renaming with greater specificity to override. I know the home for "Search this site" is search_box() in search.module, and likewise for "Read more" in node_link() in node.module. But, as these functions don't begin with theme_, I couldn't figure out any way to override them directly.
All that to say, you're simple use of str_replace() has saved me from insanity (I was moments from just throwing my hands up and changing the text in core). I am appalled that a seemingly simple operation (changing basic, persistent text across one's entire website) requires such a roundabout solution. If anyone can share the proper, "Drupalesque" solution to this issue, please do tell. Otherwise, it's str_replace() for now...
BTW, a simpler, though not as foolproof, way of making the change is to run the str_replace() directly on the $search_form (since it's just a big string anyway). That way you don't have to break down the search array and mess with printing the individual parts. Obviously, this can cause trouble though if you want to replace something generic that may show up in more instances than your desired target.
How I used your method to change the "Search this site:" text:
And also, in the same manner, for "Read more":
Hope that helps someone as much as you've helped me. And maybe we can find the "Drupal" answer to this, if there is one, sooner or later...
Replace Multi Lingual String
I use the follow Code to replace a Multi Lingual String;
Can you give me the code
Can you give me the code that needs to go in template.php please?
Thanks!
Thanks a lot - the code works fine!
.
.
It works.
Many thanks to Kayaker.
One thing needs to be mentioned: You NEED to clear the cached data (admin->site configuration->permance). It wasted more than hour before I realized this.
Brandon
Your way may not be the
Your way may not be the "right" way but I think it's just as modular as the preprocess functions way - perhaps even more so because you have direct access to the forms themselves. Anyway, I made some modifications to your code so that it will display the admin options such as spamicide, captcha, and other module stuff that your version doesn't permit.
You will also need to add search-result.tpl.php and search-results.tpl.php to your template directory to override the default.
The simplest "Drupal" way I found
I was looking to change the "Search this site" and discovered the simplest thing was to create this function in my theme's template.php file. I referenced that lullabot article mentioned above to find this.
Using preprocess functions...
As you pointed out in a later comment, this actually replaces the use of the search-block-form.tpl.php file. A better way IMHO is to use the preprocess function mechanism newly introduced in D6: A preprocess function can be put in template.php and is able to modify template variables before they are passed into the template files. In this case, one would modify the form structure of the search box and then re-render the html. The above example would then become
A local copy (in the same directory of template.php) of the search-block-form.tpl.php must also be provided, even if it isn't different from the one in the core distribution; otherwise the preprocess function isn't executed.
I've tried your method here
I've tried your method here and have moved the search-block-form.tpl.php from the core module to my local theme directory. I've added the preprocess function as well to template.php, yet I'm still seeing no changes on the site. It continues to use the default "Search this site" search block?
Any ideas? This is DP 6.4
Doing it on Drupal 6 for the search_theme_form
Thanks for sharing Marquardt. I needed to change the title of the input text of the search form for the search_theme_form, that's the one added by the theme, not the block, and used this in my template.php:
I'm using $variables instead of $vars because that's how is called in all contributed modules and core but anyway, it's just the same variable that is passed by reference.
Also, if you want to completely get rid of the title (that's the label in your HTML) replace the first line of the function above with this:
For everybody else, don't forget clearing your theme registry at /admin/settings/performance after adding a preprocess function in your theme.
Also you need to copy search-theme-form.tpl.php from /modules/search to your theme directory.
Alexis Bellido
Ventanazul: web development and Internet business consulting shouldn't be boring
Issues with preprocess
I tried the preprocess function in Drupal 6.6, and ran into an issue. I started out using the examples here, and first just tried changing the title to make sure I got everything right. After clearing cached data, I found that I had indeed successfully changed my title attribute. However, when I actually performed a search, I got unexpected results - namely, a blank, white screen. Does anyone know why this might have happened? Note that I'm trying (hard) to alter the *theme* search box, not the *block* search box. Does this matter?
Also, I read another comment about using form_alter, but one comment said this was not for themes, but for modules only. Is that right? If so, is it possible to replace my theme's search box with my own module? This method is probably overly complex for a simple change to the theme's search box, but I wanted to check anyway.
How to change Value of Submit Button
Can you tell me please how to change the default text of submit button ("Search").
Like "Find" instead of "Search" on button.
Use String Overrides
Use String Overrides to do this (http://drupal.org/project/stringoverrides)
Best,
iA
Changing the class?
I went with this method, not as simple as the 'display: none' style choice, but it was a chance to learn something new at any rate, so this was much appreciated.
I'd like to take it a step further, and it seems like I might be able to continue to leverage the preprocess function to do it, but I'm not exactly sure how to get a handle on all the available elements of search_block_form.
Specifically, I'd like to change the 'class' name of the search button element. I suppose I could create a style for 'form-submit' that matches an existing style, but I thought a more elegant solution would be to preprocess the search form(s) so their submit buttons used my style, and then I'd only have to update that style one time if it ever needed to change.
Thoughts on how I might go after that using the preprocess method?
Update: I figured it out, with a bit of stepping through some arrays and perusing the API. This is what the inside of my preprocess function looks like.
Simple way?
I just went into modules/search.module and edited the string output to suit my needs. What would be so wrong about that?
security issues? works fine on my page.
I was just offering the "Drupal way," but I found a better way
Your way works fine, but changing the core modules is not recommended as discussed earlier in this thread. If you go to update your Drupal version, that change will get wiped out.
However, I also just ran into some problems with my way, it overrides the whole .tpl.php file, so any other markup has to be duplicated in the function too.
But, I was just pointed to the stringoverrides module and am switching to use it instead of the function:
http://www.lullabot.com/articles/replace-any-string-drupal-5-6-without-l...
Definitely recommend that for replacing strings easily.
string override
lullabot.com offers some great advice on various drupal topics. I'm still going to use the non-drupal way :)
String Overrides
String Overrides
http://drupal.org/project/stringoverrides
----
Darly
Drupal 6: how to remove "Search this site"
Go to: /modules/search/search.module
Edit out the line "Search this site" from the below the lines:
function search_box(&$form_state, $form_id)
Search this site
@ allanx, you are creating your own version of Drupal thru this process. NOT recommended.
----
Darly
Not really affecting the code
Just removing the text.
eg:
function search_box(&$form_state, $form_id) {
$form[$form_id] = array(
'#title' => t('Search this site'),
function search_box(&$form_state, $form_id) {
$form[$form_id] = array(
'#title' => t(''),
Doesn't seem to affect the function, just the text displayed in the search block.
cheers
allanx
Not really affecting the code
yes! But remember to change the line again if planning to upgrade to a new version later. Actually changing core files would require submitting a patch for that module.
----
Darly
Why not just 'style' it away
#search-block-form label{
visibility: hidden;
}
I believe it's the only label used in that block.
EDIT: Actually, this is not a great solution. the tag can still cause some styling problems. It may be a quick fix for some though.
display: none to "style it away"
(of course replace or omit
#topbar
since this is special to my theme). This also removes any label in the topbar region plus all submit buttons. Also there isdiv#edit-search-block-form-1-wrapper label {}
to get this very label tag "Search this site:
".You can play around with it but the important bit is using
display: none;
instead ofvisibility: hidden;
since the later reserves space even though it's not been shown but the former doesn't even reserve space in browser's view port.Btw. thanks all you people for advices on the D6 way of creating once own search form!
P.S. I you use Opera 9.5 go to Tools -> Advanced ->Developer Tools a.k.a. Dragonfly; ng web developer (read: "themers") tool (and yes, I'm familiar with Firebug and Web Developer extensions to firefox).
Much better
Sheesh-> /me such a noob. thanks thomas. This gets my vote as the best way to omit the label until the label becomes a module configurable item. (not that it needs to be)
thanks, this worked
this worked great, thanks jackreacher
Different template name with kayaker's method
Using the template overwrite method I had to use a different file name, search-block-form.tpl.php
as documented here:
http://api.drupal.org/api/file/modules/search/search-block-form.tpl.php/6
And update all references from search_theme_form to search_block_form
Form Alter?
Guys, I'm not sure what the problem is. Can't you do this with a simple form alter in 6.0?
Ie, to change the title from 'Search this site' to 'Search' on the search_theme_form for example:
The Drupal Way (tm)
Using hook_form_alter is The Drupal Way to change your search form, IMO.
To do so, create your own module and define your own function mymodule_form_alter() with the sample code that denizengt posted. A few important notes:
Brandon
Brandon Bowersox
brandon@ojctech.com
OJC Technologies, Inc.
www.ojctech.com, 217.344-0444 ext. 14
Brandon Bowersox-Johnson, Pixo
brandon@pixotech.com
www.pixotech.com, 217-344-0444
also nice to have is the
also nice to have is the jQuery 'example' plugin where we can add a default value to the search box which will then react on focus (removing the default value). you must have jquery-plugins module and the jquery.example plugin (which has to be dropped into the jquery_plugin directory): then you add the plugin and define the preprocess as follows:
Multilingual version
Thanks to all of you. Here you are the multilingual version of the same code:
I've only set it to the "Search this site" string. If you wish changing the rest take the same path :-)
I just open search.module and
I just open search.module and removed the title search this site... worked perfectly...
Yes it does, but then you'll
Yes it does, but then you'll have to re-do it every time you upgrade Drupal. It is generally not recommended to alter the core code because of this.
The Drupal Way™ of doing this simpel label remover is ridiculously complicated, so I'll just stick with the display:none way.
when multiligual
$search['submit'] = str_replace('value="'. t('Search') .'"', 'value="'. t('Find') .'"', $search["submit"]);
Thanks people...
This code works!!!
Step by step example
My way of doing that is big, it works and is highly configurable though.
What I did was:
I will give all the code you need with comments. By commenting/uncommenting some lines you can vary between all these features.
Ok, now step by step explanation:
.
1. Preparing files
Necessary step.
Make sure you have 'template.php' file in your theme directory. If not - create one.
Copy 'search-theme-form.tpl.php' file to yout theme directory (usualy from '/modules/search').
.
2. Preparing javascript
Not necessary step, unless you want some js funcionality.
In this case I use http://www.yourhtmlsource.com/forms/clearingdefaulttext.html#theJavascript to make default value of search field dissapear when user focuses input field. You can download both needed files from the link above ('util-functions.js' and 'clear-default-text.js'). This js needs class 'cleardefault' to be added to wanted input field (we'll do that later in template.php file).
now we upload both .js files to /mytheme/js/ folder and load them in mytheme.info file:
.
3. template.php file
Necessary step.
Add this function to your template.php file:
.
4. search-theme-form.tpl.php file
Not necessary step, unless you want to change submit button to custom image.
Replace code in your search-theme-form.tpl.php file with this one:
Custom image already defined in template.php file.
.
5. Input field style
Not necessesary at all.
If you want to customize border of your input field edit your .css file. In my case it looks like:
.
6. Notes
Please pay attention.
First of all thanks for many of you who commented above - I used a lot of your code :)
To configure everything read carefully comments in template.php file - it just looks big actualy its really simple. Experiment a little and you'll get the result you want.
Sorry for my english .)
and if you noticed any errors or have an improvement - share it with others below!
Removing only the wrapper DIVs
Ho to all
considering all above...
How to remove ONLY the wrapper DIV inside the FORM Tag, using template.php...??
These are the rendered DIV by a
print $search_box
SnippetHow can I "destroy" them permanetly? ;)
I need Only FORM + LABEL + INPUT(s) TAGS without unusefull, nasty container!
Thanks!
Bye!
Done
Done this:
My solution: rebuild the form via template
This is my way but I don't know if could have security problem:
I re-build the form via template adding some PHP snippet without string replacements functions or override:
My Search form now is something like this:
The markup is now small, sematic (no unusefull
Bye.
i'm a little bit confuse...
i'm a little bit confuse... please provide what template file you've modify to remove those extra DIV's? is it the template.php, search-theme-form.tpl or search-block-form.tpl?
i've tried this method... and
i've tried this method... and it output the search form... but when i try to put some keywords... their were no possible results returned. what could be the missing?
closeResults()?
@sakib000: Even if I agree that we should not use tpl.php for overriding a search form layout, I use the same tweak.
BTW, what does your "closeResults()" function do?
--
www.jazzit.hr
Quick and dirty!
How I did it, this way you are language independent and it doesn't matter what could be changed in the future within any part of the search.
search-block-form.tpl.php
in your themes folder. You can use the following code:block-search.tpl.php
in your themes folder. You can use the following code:Custom Search Module
The custom search module allows you to change or remove the label text and set the image path for a custom submit button.
http://drupal.org/project/Custom_Search
can this module remove some
can this module remove some extra tags like some unwanted div's? theming method to be exact...
Maybe with template.php
You can do it with the function yourtheme_preprocess_search_block_form() in your template.php file of your theme :
This works for me.
Raphael
My Combination of All Proposed Solutions
I spent a few hours messing around with each and every solution presented, and the ideal solution I found for having a search field that says "search" that disappears 'onclick' and a submit button that uses an image and has no text is as follows:
First, use the code that is pasted below and pop it into your template.php file. This is pulled from some other posts and tweaked a little by me, so all thanks go to the community for providing the backbone on this one, and I take no credit for it.
Second, to make your submit button into an image simply style your submit button using the following css (I used a container div called searchwrap in this example):
You can also use div#searchwrap label { display: none } to get rid of any labels you don't want.
Third, reset your site cache by going to admin/settings/performance and click "clear cached data."
This is what I found to be the ideal solution to having a small, simple search box and submit button image. The two major pluses I see are a) you have much more control over your submit image by using css, such as adding hover properties, and b) you don't clutter up your theme folder with template override files.
Hello steel-track, Can I
Hello steel-track,
Can I apply this code to a Zen sub-theme as well please?
I have inserted this code in my theme.css but for some reason, the submit button moves back where it was originally:
How can I make sure that it won't move? Any idea?
Thanks a lot,
My solution need to activate
My solution (posted here:http://drupal.org/node/232874#comment-2461684) need the "search block" in theme admin page to be active. After You have also to remove the original snippet for it and place my code everywhere you need in page .tpl file.
Handbook: How to Customize the Block Search Form
This long thread is neatly summarized in the top part of this Handbook page:
How to Customize the Block Search Form - http://drupal.org/node/154137
Subscribe
Subscribe
SEARCH help, not the form, the "printed' text on the page
Good day,
I would think this a simple task, but I know nothing of php, and am new to drupal. My search form prints out the word "SEARCH" right above the search field on my site. It is throwing off the alignment of the text and the field itself. I used firebug to inspect an element, and found that if I removed this code
Search
, the output of "SEARCH" went away.
However, I do not know which file to edit in my theme or in the search module itself to remove this output. It seems like it would be simple, but it is not due to my ignorance.
I do not wish to change the form elements or the presentation of the search form itself, only the text that prints above the form. Can anyone help? PLEASE??