Overriding function search_box in template.php...

purrin - April 5, 2008 - 09:13

I simply want the ability to remove the default text "Search this site: " from the $search_box variable when theming in Drupal 6, as well as possibly changing the submit button's text from "Search" to "Go!" or something like that... The way I think this function should be overridden is below. I can't see why it's not working. There are no changes being output to the screen, but I know it is at least looking at my template.php file because if I don't add the phptemplate_ prefix to the function name is gives me an error saying I am redeclaring an existing function, basically.

Any ideas why this is not working?

-=- christopher

/**
* Form builder; Output a search form for the search block and the theme's search box.
*
* @ingroup forms
* @see search_box_form_submit()
* @see theme_search_box_form()
*/
function phptemplate_search_box(&$form_state, $form_id) {      //  ADDED the phptemplate_ override name appendage to search_box
  $form[$form_id] = array(
    '#title' => t(''),                                     // REMOVED THE 'Search this site:' text from this array element value
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Go!'));      // CHANGED 'Search' to 'Go!' in the array element here
  $form['#submit'][] = 'search_box_form_submit';
  $form['#validate'][] = 'search_box_form_validate';

  return $form;
}

Also would like to see how

kyrylkov - April 6, 2008 - 19:55

Also would like to see how to override this function properly.

someone is bound to know on

purrin - April 7, 2008 - 05:26

someone is bound to know on here... *crosses fingers*

-=- christopher

my Las Vegas blog

Customizing Search Block

coupet - April 11, 2008 - 17:09

Customizing Search Block Form in Drupal 6.0
http://drupal.org/node/232874

----
Darly

As I understand it, the

purrin - April 12, 2008 - 06:39

As I understand it, the $search_box and search block are two different things...

functions from Search mdule

coupet - April 12, 2008 - 19:42

function search_forms() {
  $forms['search_theme_form']= array(
    'callback' => 'search_box',
    'callback arguments' => array('search_theme_form'),
  );
  $forms['search_block_form']= array(
    'callback' => 'search_box',
    'callback arguments' => array('search_block_form'),
  );
  return $forms;

function search_theme() {
  return array(
    'search_theme_form' => array(
      'arguments' => array('form' => NULL),
      'template' => 'search-theme-form',
    ),
    'search_block_form' => array(
      'arguments' => array('form' => NULL),
      'template' => 'search-block-form',
    ),
    'search_result' => array(
      'arguments' => array('result' => NULL, 'type' => NULL),
      'file' => 'search.pages.inc',
      'template' => 'search-result',
    ),
    'search_results' => array(
      'arguments' => array('results' => NULL, 'type' => NULL),
      'file' => 'search.pages.inc',
      'template' => 'search-results',
    ),
  );
}

----
Darly

How I changed the Search Box in Drupal 6

coupet - April 12, 2008 - 19:45

How I changed the Search Box in Drupal 6
http://drupal.org/node/232874#comment-779380

----
Darly

thanks to Darly!

purrin - April 13, 2008 - 09:57

With Darly's links to pointers to some information I had not run across previously, I was finally able to put things I had seen in several different places together to make exactly what I needed to happen happen in the "right way" for me... or as close to it as I imagine happening any time soon.

It does seem to me that there should be a way to update the variable for "Search this site:" and others directly /before/ the system renders the page. That would be a "better," more efficient way of going about things, rather than rendering the page the way you do not want it and then string-replacing to hack it into shape at the last possible moment before final output to the browser... but at least now, with the technique below, it is more properly tucked away inside of the search-theme-form.tpl.php instead of in your page.tpl.php file as some had suggested. That just doesn't make a lot of sense to me because it puts more back-end logic right in the presentation layer of things, but sometimes, just getting work done is better than doing it the "best way," I suppose.

Most of the code is from a link down below that Darly posted. The original code in the middle section does an image replacement of the search button, which is very cool. For my purpose, I simply needed Search button to say "Go!" and the "Search this site: " text above the input box to disappear completely, so I modified the code a bit to remove the text altogether and added a second like to do a string replacement of the Search text on the button itself, while commenting out the valuable image replacement code so that I can add this $search_box override template to my personal set of Drupal theming code snippets and use it for different types of situations. I bet this will come in handy for others in the future... it sure solved an ongoing problem for me!

<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"]);
 
  //    * Replace "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>

And the label?

hass - June 14, 2008 - 08:14

Your example does not remove the label tag. This one should solve this issue.

<?php
// $Id: search-theme-form.tpl.php,v 1.0 2008/06/14 10:10:00 hass Exp $
?>

<div id="search" class="container-inline">
  <?php
  $search
['search_theme_form'] = preg_replace('/<label(.*)>(.*)<\/label>\n/i', '', $search['search_theme_form']);
  print
$search['search_theme_form'];
  print
$search['submit'];
  print
$search['hidden'];
 
?>

</div>

Thanks for a great tutorial.

kaido24 - September 11, 2009 - 13:09

Thanks for a great tutorial. But can you make this work with multilingual sites? I got this to work when I removed translation strings but still I think there is a better way ;)

I got this to work now!

Thanks you both Purrin and Hass :)

 
 

Drupal is a registered trademark of Dries Buytaert.