Drupal Tip #5 – Custom Theme Function for Forms

Defining a custom theme function for forms involves writing the actual theme function and registering it with your own hook_theme function.

Custom Theme Function

The name of the function would be your form ID (replacing dashes with underscores) prepended by ‘theme_’:

function theme_your_form_id($form){
$output = 'Any html you need here';
$output .= drupal_render($form['subject']);
$output = ' and here';
$output .= drupal_render($form['submit']);
$output .= drupal_render($form);
$output .= '';
return $output;
}

Registering the theme function

In your hook_theme implementation, add a key to the returned array. The key should be your form id, do not include the ‘theme_’ prefix.

function deepgreen_theme() {
$themes = array(
. . . other theme functions here . . .
'your_form_id' => array (
'arguments' => array('form' => NULL),
'type' => 'module',
),
);
return $themes;
}

Leave a Reply

Your email address will not be published. Required fields are marked *