Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By Ken Collins on
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
Anyone????
Seems like all the trolls get the attention :/
quick one..
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
Step 2 of 2 - Your search_box.tpl.php file
This is a default search_box layout for Drupal 4.7:
Hope that helps
Dub
Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate
That works... but...
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?
quick one
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.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
Thanks again...
The second item is what I'm really interested in!! I'll dig around some more and try reading up more.
This is exactly what I'm looking to do too
"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 ~*
And how about the comment form?
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
Text of the button is not translatable ...
... please give me a hint what to do for a translatable button text.
closing </form> but no opening?
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?
Search Template Custom
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
Form Items Unthemable?
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?
You're wrong as you
You're wrong as you expected. Search form.inc for theme_* functions.
Borek, http://borber.com/
There is no theme_search_box in drupal4.7.rc3
so catching of this function doesn't work anymore...
This function was removed from system.module in rc3 version.
Just updated the handbook page
Use
Then you can do a lot in search-box.tpl.php; you can even play with the forms array, eg:
(edit: fixed typo (from below))
--
When your problem is solved, please post a follow-up to the thread you started.
typo
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
Thanks
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...
Can I do this with CSS alone?
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
Drupal development and themeing, Galway, Ireland
hide
add this to your css file
introfini
josefernandes.pt
José Fernandes
Bloomidea
Not quite
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
Drupal development and themeing, Galway, Ireland
Use hook_form_alter
I suggest using hook_form_alter(). As an example, here's how I disabled the advanced search entirely:
If you want to disable individual elements, you just need to unset the corresponding parts in $form['advanced'].
Where would this function go?
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
Drupal development and themeing, Galway, Ireland
It goes in a module.
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:
";
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.
Thanks
I'll give that a try when I get time.
Drupal development and themeing, Galway, Ireland
Search config
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.
I have no idea what I'm
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
Solved
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 :)
following handbook crashes my site
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
to my template.php
with
in my search-block-form.tpl.php form
http://www.varnelis.net
following handbook crashes my site
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
to my template.php
with
in my search-block-form.tpl.php form
Any ideas?
http://www.varnelis.net