09
Nov

Creating a theme for a form in Drupal 6 is slightly different from Drupal 5.

Steps on how to theme a form in Drupal 6:

  1. Register the theme file.
    Use the hook_theme() function to register the theme for  the form. More information on hook_theme() can be got from here: http://api.drupal.org/api/function/hook_theme

    NOTE: myform1_form and myform2_form are the form ids of the forms you want to theme
    function mymodule_theme()
    {
        $themes = array(
            'myform1_form' => array(
                'arguments' => array('form'),
                'function' => 'mytheme_myform1_form',
            ),
            'myform2_form' => array(
                'arguments' => array('form'),
                'function' => 'mytheme_myform2_form',
            ),
        );
        return( $themes );
    }
  2. Define the callback function. Pass parameters to the theme file and render the file.
    function mytheme_ myform1_form($form)
    {
        drupal_add_css(drupal_get_path('module', 'mymodule') .'/mymodule-form.css');
        drupal_add_js(drupal_get_path('module', 'mymodule') .'/mymodule-form.js');
        return theme_render_template(path_to_theme() . '/mytemplate.tpl.php', array('form' => $form));
    }
    NOTE: It is important to pass the form parameter to the template file as array('form' => $form)
    instead of directly passing $form
    For more information on theme_render_template() check this http://api.drupal.org/api/function/theme_render_template/6
  3. Template file.
    <div class="onelinedd">
        	<div class="onedd">
            	<div class="labels">
     		  <div>Form Field 1</div>
     		  <?php echo drupal_render($form['form_field_1']); ?>
     		</div>
            </div>
        	<div class="onedd">
            	<div class="labels">
     		  <div>Form Field 2</div>
     		  <?php echo drupal_render($form['form_field_2']); ?>
     		</div>
            </div>
        	<div class="onedd">
     	  <?php echo drupal_render($form['submit']); ?>
            </div>
        </div>
  4. Clear cache. Admin -> Site Configuration -> Performance -> Clear Cache

I am sure there must be an better way to do this but, after hours of searching and trying this is what worked for me!

Tips on how to theme a form were taken from these links:

http://agaric.com/note/how-theme-drupal-form

http://drupal.org/node/228189

http://api.drupal.org/api/function/hook_theme

http://api.drupal.org/api/function/theme_render_template/6

, , , , ,

gayatri.sa
Posted by on 09 Nov 2009 by gayatri.sa in Drupal

One Response to “How to theme a form in Drupal 6”

  • hi could you please provide me with some more info about the

    function mymodule_theme()
    {
    $themes = array(
    ‘myform1_form’ => array(
    ‘arguments’ => array(‘form’),
    ‘function’ => ‘mytheme_myform1_form’,
    ),
    ‘myform2_form’ => array(
    ‘arguments’ => array(‘form’),
    ‘function’ => ‘mytheme_myform2_form’,
    ),
    );
    return( $themes );
    }

    and the ‘/mytemplate.tpl.php’ . . .
    for example . .
    i have a node-type called : boxes
    my theme is called : multiflex3
    how do i replace these values?
    thanx !

Add reply

*