Friday, June 17, 2011

How to theme a form

function vs. file
Well, assuming the form doesn't add already a theme implementation, you have 2 ways to override the form rendering with hook_theme:

function in your template.php
file in your template directory

To add a function you can either specify a 'function' value in the array or leave empty (according hook_theme)

function: If specified, this will be the function name to invoke for this implementation. If neither file nor function is specified, a default function name will be assumed. For example, if a module registers the 'node' theme hook, 'theme_node' will be assigned to its function. If the chameleon theme registers the node hook, it will be assigned 'chameleon_node' as its function.

So this is the code:

array(
'arguments' => array('form' => NULL),
),
);
}

function MYTHEME_MYFORM_form($form) {
$form['#title'] = 'New form title'; // minimal change but you can do anything here
return drupal_render($form);
}
?>

BTW changes like the one above would be better handled on a form_alter hook...

On the other hand if you want to play with a file you should enter a value for the 'template' key:

template: If specified, this theme implementation is a template, and this is the template file without an extension. Do not put .tpl.php on this file; that extension will be added automatically by the default rendering engine (which is PHPTemplate). If 'path', above, is specified, the template should also be in this path.

So the code would be:

array(
'arguments' => array('form' => NULL),
'template' => MYFORM,
),
);
}
?>

Then the template file would be MYFORM.tpl.php.

No comments:

Post a Comment