Themeing the search form

Submitted by Frederic Marand on

In the recently introduced Drupal RC2, a pair of new functions have appeared: theme_search_block_form() and theme_search_theme_form(). They replace the ephemeral (some said "Easter Egg") theme_search_box() appeared and disappeared between RC1 and RC2, and raise important issues for themers.

Themeing the search form block

For now, I'll just skip theme_search_theme_form(), and consider theme_search_block_form().

As its name implies, its goal is to allow themes to overload the default look of the search form within the search block provided by search.module, just like theme_search_box() did.

So what could be done with it ? First idea is to change the caption of the button submitting the search. Let's do this ; based on the code for theme_search_block_form(), it should look like this:

<?phpfunction mytheme_search_block_form($form)   {  $form['submit']['#value'] => 'ok';  return '<div class="container-inline">'       . form_render($form)        . '</div>';  } ?>

Simple enough. Let's try this... Hello ? Nothing seems to work. Submitting the form returns you to the current page. What's happening ?

Themeing the search form block, redux

As explained by chx, the 4.7 branch of Forms API is heavily dependent on the caption used on submits, for all forms. An easily understandable example appears in node.module/function node_form_add_preview($form, $edit): look it up.

So what's the workaround: simple enough, keep the default caption, but hide it. A CSS-independent way of achieving this is to cast the default submit to a hidden field and add a new field in the theme (suggestion by UnConeD) :

<?phpfunction mytheme_search_block_form($form)   {  $form['submit']['#type'] => 'hidden';  $form['submit2'] = array    (    '#type'  => 'submit',    '#value' => t('A new caption for search in this theme'),    );  return '<div class="container-inline">'       . form_render($form)        . '</div>';  } ?>

That way, FAPI receives the expected submit text on which it depends, while the user sees the themed version of the form. works nicely in 4.7 RC2. Until the next release ?

Back to the future

Obviously, this type of workaround is kludgy: to a reasonable extent, the operation of a form should not depend on themeing, and especially not on captions, which are just a documentation/look element.

A suggestion by chx for the 4.8 branch is to have op as an associative array, in which the keys would be the things that Drupal cares for, instead of just a string as is currently the case. By default, of course, the value would be equal with the keys but could still be overloaded without affecting form behaviour. This will have to mature, but seems promising.