Customizing Search Block Form in Drupal 6.0

ardarvin - March 11, 2008 - 17:02

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

Nope...the code from the

ardarvin - March 12, 2008 - 23:54

Nope...the code from the aforementioned doc didn't work in Drupal 6.

Anyone?

take a look at a d6 theme already doing it..

zilla - March 13, 2008 - 01:11

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

ardarvin - March 21, 2008 - 01:27

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.
*
*

<?php
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

kayaker - March 22, 2008 - 20:11

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:

<?php
print '<pre>'. check_plain(print_r($search, 1)) .'</pre>';
?>

3. In theme copy of search-theme-form.tpl.php, removed

<div id="search" class="container-inline">
  <?php print $search_form; ?>
</div>

and replaced it with
<div id="search" class="container-inline">
  <?php

 
/* remove ' this site' - probably should try more surefire way using </label> */
 
$search["search_theme_form"]= str_replace(" this site", "", $search["search_theme_form"]);

 
/* Replace button input type with image  */
 
$search["submit"]=str_replace('input type="submit"', 'input type="image" src="yoursite-path-to-your-search.png" ', $search["submit"]);

  print
$search["search_theme_form"];
  print
$search["submit"];
  print
$search["hidden"]; 
  
  
?>

</div>

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.

ardarvin - March 24, 2008 - 21:59

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?

zilla - March 24, 2008 - 22:50

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

ardarvin - March 24, 2008 - 23:04

So I got the javascript to work...I just needed to absolutely link to the files in the head.

ie:

  <script type="text/javascript" src="http://mysite/themes/mytheme/util-functions.js"></script>
  <script type="text/javascript" src="http://mysite/themes/mytheme/clear-default-text.js"></script>

Using str_replace to alter

ardarvin - April 2, 2008 - 16:56

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

siluet - April 7, 2008 - 18:58

I find a great article about modifiying forms:

http://www.lullabot.com/articles/modifying-forms-5-and-6

I updated the code you had

purrin - April 13, 2008 - 09:30

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

<div id="search" class="container-inline">
  <?php

 
//    * Remove "Search this site: "  *    ////
 
$search["search_theme_form"]= str_replace("Search this site: ", "", $search["search_theme_form"]);

 
//    * Replace button input type with image  *      ////
  //    $search["submit"]=str_replace('input type="submit"', 'input type="image" src="yoursite-path-to-your-search.png" ', $search["submit"]);
 
  //    * "Search" button with "Go!" *      ////
 
$search["submit"]=str_replace('value="Search"', 'value="Go!"', $search["submit"]);



  print
$search["search_theme_form"];
  print
$search["submit"];
  print
$search["hidden"];
 
  
?>

</div>

I too am a Drupal newb, but

cade_t - April 18, 2008 - 15:49

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:

<?php

   
/* edit 'this site:' */
   
$tweaked_search_form = str_replace("this site:", "name of site", $search_form);

    print
$tweaked_search_form;

?>

And also, in the same manner, for "Read more":

  1. Go to node.tpl.php
  2. If there's an an if statement for $links, add code inside statement, else simply add if statement
  3. Add the following code

<?php if ($links): ?>
    <?php

       
/* edit 'Read more' */
       
$tweaked_links = str_replace("Read more", "[ more... ]", $links);

   
?>

      <div class="links"><?php print $tweaked_links; ?></div>
    <?php endif; ?>

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

ozon - May 16, 2008 - 11:11

I use the follow Code to replace a Multi Lingual String;

<?php
$search
["search_theme_form"]= str_replace(t('Search this site').':', "<you new stringt or leave it blank for none>", $search["search_theme_form"]);

...
...
?>

Can you give me the code

sus - May 27, 2008 - 22:49

Can you give me the code that needs to go in template.php please?

The simplest "Drupal" way I found

mountaineer - April 24, 2008 - 16:36

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.

/**
* Override the search block form so we can change the label
* @return
* @param $form Object
*/
function phptemplate_search_block_form($form) {
  $output = '';
 
  // the search_block_form element is the search's text field, it also happens to be the form id, so can be confusing
  $form['search_block_form']['#title'] = t('some crazy search title');

  $output .= drupal_render($form);
  return $output;
}

Using preprocess functions...

marquardt - June 27, 2008 - 21:51

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

/**
* Override or insert PHPTemplate variables into the search_block_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/
function phptemplate_preprocess_search_block_form(&$vars, $hook) {

  // Modify elements of the search form

  $vars['form']['search_block_form']['#title'] = t('some crazy search title');

  // Rebuild the rendered version (search form only, rest remains unchanged)

  unset($vars['form']['search_block_form']['#printed']);
  $vars['search']['search_block_form'] = drupal_render($vars['form']['search_block_form']);

  // Collect all form elements to make it easier to print the whole form.

  $vars['search_form'] = implode($vars['search']);

}

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.

Simple way?

GetActive - April 24, 2008 - 18:15

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

mountaineer - April 24, 2008 - 18:18

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

GetActive - April 24, 2008 - 18:41

lullabot.com offers some great advice on various drupal topics. I'm still going to use the non-drupal way :)

String Overrides

coupet - April 24, 2008 - 18:54

String Overrides
http://drupal.org/project/stringoverrides

----
Darly

Drupal 6: how to remove "Search this site"

allanx - May 28, 2008 - 07:42

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

coupet - May 28, 2008 - 15:13

@ allanx, you are creating your own version of Drupal thru this process. NOT recommended.

----
Darly

Not really affecting the code

allanx - May 29, 2008 - 05:38

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

coupet - May 29, 2008 - 12:54

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

jackreacher - May 29, 2008 - 18:59

#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"

thomas23@drupal.org - May 30, 2008 - 10:43

  #topbar label,
  #topbar #edit-submit,
  #topbar #edit-submit-1,
  #topbar #edit-submit-2
  {
    display: none;
  }

(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 is div#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 of visibility: 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

jackreacher - May 30, 2008 - 16:30

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)

 
 

Drupal is a registered trademark of Dries Buytaert.