I am trying to theme the search_box() function in the system.module of 4.7 and I need to add a different CSS #ID property other than the default. I can easily see the theme_search_box() function and it's easy to pass that into template.php, but I need to change some fo the form items. How do I do this with PHPTemplate?

BTW, I just read the Forms API and the Quickstart guide, but still this was not obvious. For instance, I tried just creating a copy of both and adding 'my' infront of it and slapped this into template.php, but to no avail. Help?

function mysearch_box() {
  $form['#action'] = url('search');
  $form['keys'] = array('#type' => 'textfield', '#size'=> 15, '#value' => '', '#attributes' => array('alt' => t('Enter the terms you wish to search for.')));
  $form['submit'] = array('#type' => 'submit');
  return drupal_get_form('search_box', $form);
}

function theme_mysearch_box($form) {
  $output = '<div id="search" class="container-inline">';
  $output .= form_render($form);
  $output .= '</div>';
  return $output;
  }

Comments

Ken Collins’s picture

Seems like all the trolls get the attention :/

Dublin Drupaller’s picture

Hi Ken,

Just a quick one..but, I was looking at Drupal 4.7 search stuff recently and while it might be easier to just edit the page.tpl.php file...

Step 1 of 2 - Template.php snippet to override the search_box

<?php
function phptemplate_search_box() {
 /**
 * This snippet catches the default search_box layout
 * and looks for a search_box.tpl.php file in the same folder
 * which has the new layout.
 */
return _phptemplate_callback('search_box');
  }
?>

Step 2 of 2 - Your search_box.tpl.php file

This is a default search_box layout for Drupal 4.7:

<div><div id="search" class="container-inline"><div class="form-item">
 <input type="text" maxlength="128" class="form-text" name="edit[keys]" id="edit-keys"  size="15" value="" alt="Enter the terms you wish to search for." />
</div>
<input type="submit" class="form-submit" name="op" value="Search"   />
<input type="hidden" name="edit[form_id]" value="search_box"   />
</div>
</div></form>

Hope that helps

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Ken Collins’s picture

This goes to show that I was overthinking the issue. My thoughs were that if I hooked into the function that was generating the form, I could always be compatible with the output that core wants to give this form.

BTW, I could not edit my page.tpl.php file because it did not have the id="edit-keys" attribute for the generated form input field. That's why I was going to the Form API to over ride the default values. QUESTION: Is it possible to theme drupals form using the Form API without writing a module?

Dublin Drupaller’s picture

BTW, I could not edit my page.tpl.php file because it did not have the id="edit-keys" attribute for the generated form input field. That's why I was going to the Form API to over ride the default values.

In Drupal 4.7, you can simply replace the print $search_box in your page.tpl.php file with the second snippet I posted and just edit/tweak it from there.

QUESTION: Is it possible to theme drupals form using the Form API without writing a module?

As I understand it Drupal & PHP template allows you to override every themable function, so, Yes, you can.

hope that helps

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Ken Collins’s picture

The second item is what I'm really interested in!! I'll dig around some more and try reading up more.

jenlampton’s picture

"My thoughts were that if I hooked into the function that was generating the form, I could always be compatible with the output that core wants to give this form."

This is exactly what I'm looking to do too... I need to use the function that generates the form because there are other modules that tap into that function and produce form objects for users to search on. If I replace that dynamic method with hard coded select objects, the form will not update itself when the site is changed.

Did you ever find out how to theme the output of that function?
Jen

*~ current project: www.customerthink.com ~*

tangibre’s picture

Is that as easy to theme. I have looked into the html output from the generated form, and thought of copying this to a snippet, but I guess there was some problems regarding the timestamp etc.

Any ideas?

Hans

B.GARBE’s picture

... please give me a hint what to do for a translatable button text.

WillieBHines’s picture

How come this snippet (and also on the "Customising the search box" page http://drupal.org/node/45295) your snippet has a closing </form> tag but no opening one? Is that a mistake?

coupet’s picture

ref: drupal 5.x

How to change the default number of returned nodes in listings when searching?
the default value is 10. How can this number be changed?

The reference solution suggest changing search.module code.
Is there a recommended solution that does not require changing search.module?

Increase number of returned nodes?
http://drupal.org/node/79744#comment-146959

----
Darly

bryan kennedy’s picture

It seems to me that there isn't a way to theme the actual output of the Forms API. Like if it calles a radio button there is no way to get inside that radio button code. Is this right? The theme functions seem to give you the ability to modify code around a drupal form item but not actually change any of the output inside a specific form call.

I hope I am wrong. Does anyone know how to do this?

Borek-1’s picture

You're wrong as you expected. Search form.inc for theme_* functions.

Borek, http://borber.com/

nbayaman’s picture

so catching of this function doesn't work anymore...
This function was removed from system.module in rc3 version.

Heine’s picture

Use

function phptemplate_search_theme_form($form) {
 /**
  * This snippet catches the default searchbox and looks for
  * search-box.tpl.php file in the same folder
  * which has the new layout.
  */
  return _phptemplate_callback('search-box', array('form' => $form));
}

Then you can do a lot in search-box.tpl.php; you can even play with the forms array, eg:

  print form_render($form);

(edit: fixed typo (from below))
--
When your problem is solved, please post a follow-up to the thread you started.

rkendall’s picture

return _phptemplate_callback('search-box', array('form' => $form);
missing a ')' at the end

try:
return _phptemplate_callback('search-box', array('form' => $form));

or better yet try:
return _phptemplate_callback('search_theme_form', array('form' => $form));
and name the 'tpl' file: search_theme_form.tpl.php

Heine’s picture

Thank you for spotting & correcting the typo. Now if I would be able to find the handbook page, I could check if it required correction there too...

alanburke’s picture

I am trying to configure the search for a Drupal site.
I am trying to do two seperate things, with no luck so far.

I'd like to be able to link directly to the 'advanced search' option, rather than having to toggle it on by clicking on 'Advanced search'. I can't see a link that goes directly to this.
Any clues?

As a seperate issue, I don't want the users to be able to see the section of the advanced search form: 'Only of the type(s):'.

Is it possible to hide this via CSS, or do I have to dig into the search module code[would rather not touch this].

Regards,
Alan

introfini’s picture

add this to your css file

.criterion{
	display: none;
}

introfini
josefernandes.pt

José Fernandes
Bloomidea

alanburke’s picture

Thanks for the help.
Unfortunately this hides all of the 'advanced search' functionality.
All of the sections must be of the class 'criterion'.

I Just need to be able to hide which content types can be chosen to search in.

Regards
Alan

datura-1’s picture

I suggest using hook_form_alter(). As an example, here's how I disabled the advanced search entirely:

  if('search_form' == $form_id) {
    // disable advanced form
    unset($form['advanced']);
  }

If you want to disable individual elements, you just need to unset the corresponding parts in $form['advanced'].

alanburke’s picture

I'm at the edge of my Drupal coding abilities here so apologies..

Where would I put this function?
How could I work out which items I need to put into the 'unset ' piece so that I could hide the 'only of these types' option?

Regards
Alan

datura-1’s picture

To use a hook like hook_form_alter, you need to place the function in a module.

The Module developer's guide is very helpful if you've not seen it: http://drupal.org/node/508

In terms of figuring out which variables to unset, You could temporarily add this to your form_alter hook:

 echo "<code>";
 print_r($form);
 echo "\n

";

This will print the $form array and you will be able to see all of the items in the Advanced Search section of the form. Hope that helps.

alanburke’s picture

I'll give that a try when I get time.

canen’s picture

Pimping my own untagged module at the moment. I did a module, search config, that makes configuring the advanced search form pretty easy.

It works with 4.7 but I have not tagged a release yet. I would appreciate some testing though.

tormu’s picture

I have no idea what I'm doing wrong, but doing the thing exactly like it was instructed at the book page doesn't change anything on the search form..

I added the callback-function to the template.php and made a new file called search-theme-form.tpl.php and copypasted the default-layout there and altered it, but the changes don't take affect at all.
The function on the template.php does get called since putting some print there outputs it.. but the template-file doesn't seem to overwrite the default layout.

Drupal 4.7.2

tormu’s picture

Ok, seems that I was trying to theme the wrong element :P

all that was needed in this case was to change the function name into phptemplate_search_block_form at the template.php, instead of phptemplate_search_theme_form

Thanks Heine for pointing it out on IRC :)

kvarnelis’s picture

I get Parse error: syntax error, unexpected T_STRING in /home/.philie/kazys/audc.org/sites/networkedpublics.org/themes/netpublics2007/template.php on line 8

when I add

<?php
function phptemplate_search_block_form($form) {
  /**
   * This snippet catches the default searchbox and looks for
   * search-block-form.tpl.php file in the same folder
   * which has the new layout.
   */
  return _phptemplate_callback('search-block-form', array('form' => $form));
}
?>

to my template.php

with

<label for="search_block_form_keys">Custom Search</label>
<input type="text" maxlength="128" name="search_block_form_keys" id="edit-search_block_form_keys"  size="25" value="" title="Enter the terms you wish to search for." class="form-text" />
<input type="submit" name="op" value="Search"  />
<input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form" />
<input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_block_form'); ?>" />

in my search-block-form.tpl.php form

http://www.varnelis.net

kvarnelis’s picture

I get Parse error: syntax error, unexpected T_STRING in /home/.philie/kazys/audc.org/sites/networkedpublics.org/themes/netpublics2007/template.php on line 8

when I add

<?php
function phptemplate_search_block_form($form) {
  /**
   * This snippet catches the default searchbox and looks for
   * search-block-form.tpl.php file in the same folder
   * which has the new layout.
   */
  return _phptemplate_callback('search-block-form', array('form' => $form));
}
?>

to my template.php

with

<label for="search_block_form_keys">Custom Search</label>
<input type="text" maxlength="128" name="search_block_form_keys" id="edit-search_block_form_keys"  size="25" value="" title="Enter the terms you wish to search for." class="form-text" />
<input type="submit" name="op" value="Search"  />
<input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form" />
<input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_block_form'); ?>" />

in my search-block-form.tpl.php form

Any ideas?

http://www.varnelis.net