Home > Drupal > Drupal 6 theming a fieldset

Drupal 6 theming a fieldset

  • Drupal 6 fieldsets can be themed by supplying the #theme attribute for the fieldset
  • #theme = your custom fieldset theming function
  • you need to declare this theme function in the modules hook_theme function
  • drupal will automatically look for theme_your_fieldset_theme_function()
  • see the complete working example below
/**
 * hook_menu
 * @return array
 */
function test_menu() {

    $items = array();

    $items['test'] = array(
        'title' => 'Test',
        'page callback'	=> 'drupal_get_form',
        'page arguments' => array('test_form'),
        'access callback' => true,
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

/**
 * hook_theme
 * theme registry - register all theme functions here
 * "theme_" is added to all the functions
 *
 * @return array
 */
function test_theme() {
    return array(
        'test_form' => array( 'arguments' => array(),),
        'address_fieldset' => array( 'arguments' => array(),),
    );
}

/**
 * form with fieldset
 * @return array
 */
function test_form() {

    $form = array();

    // first name
    $form['first'] = array(
        '#type' => 'textfield'
    );

    // last name
    $form['last'] = array(
        '#type' => 'textfield'
    );

    // address fieldset
    $form['address'] = array(
        '#type' => 'fieldset',
        '#title' => 'Address',
        '#collapsible' => true,
        '#collapased' => false,
        '#theme' => 'address_fieldset' // declare this in the module theme registry
    );

    // city
    $form['address']['city'] = array(
        '#type' => 'textfield'
    );

    // zip
    $form['address']['zip'] = array(
        '#type' => 'textfield'
    );

    // submit
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );

    return $form;
}
/**
 * theme function for the form
 * @param $form
 * @return string
 */
function theme_test_form($form) {
     $output = "<table>
         <tr><td>First Name: </td>
             <td>".drupal_render($form['first'])."</td>
         </tr>
         <tr><td>Last Name: </td>
             <td>".drupal_render($form['last'])."</td>
         </tr>
     </table>";
     // render the fieldset
     $output .= drupal_render($form['address']);
     // render the rest of the form
     $output .= drupal_render($form);
     return $output;
}

/**
 * theme function for the address fieldset
 * @param $form
 * @return string
 */
function theme_address_fieldset($form) {
    $output = "<table>
        <tr><td>City: </td>
            <td>".drupal_render($form['city'])."</td>
            <td>Zip: </td>
            <td>".drupal_render($form['zip'])."</td>
        </tr>
    </table>";
    return $output;
}


Categories: Drupal
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: