In this example, we are theming Drupal form different from its default layout. Suppose we have a search form with two fields search textbox and category selectbox. We are assuming that our module name is “search“.

Default Layout Search Form
Themed Layout Search Form
Step 1. Create search form using Drupal Form API.
function search_form() { $form['str'] = array( '#type' => 'textfield', '#size' => '32', ); $form['category'] = array( '#name' => t('category'), '#type' => 'select', '#options' => array('0' => t('All'), '1' => t('Bars'), '2' => t('Restaurants')), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Search'), ); return $form; }
Step 2. Register our theme in Drupal theme registry using hook_theme().
function search_theme() { global $theme; return array( 'search_form' => array( 'arguments' => array('form' => NULL, 'theme' => $theme), 'template' => 'search-form', ), ); }
Step 3. Create search-form.tpl.php file to theme this search form.
<table cellspacing="0">
<tr>
<td class="container-inline"><strong><?php print t('Search');?></strong> <?php print drupal_render($form['str']); ?></td>
<td class="container-inline"><strong><?php print t('Category');?></strong> <?php print drupal_render($form['category']); ?></td>
<td><?php print drupal_render($form['submit']); ?></td>
</tr>
</table>
<?php
print drupal_render($form);
?>
#1 by Jim - May 18th, 2009 at 19:53
Added to DrupalSightings.com
#2 by Andrew - June 2nd, 2009 at 22:28
Where should one place the template php file? Does that go inside of the “theme” folder within a given module folder?
Also, the current module that I would like to apply this to has multiple pages that are defined using the hook_menu(). How would one apply the theme template to just one of the pages within a module?
#3 by siva - November 9th, 2009 at 12:36
how to display the validation and auto completed to the text filed.