Drupal Form Theming Quick Tip

Very quickly, here's a useful tip if you're theming any kind of Drupal form. Just put the following in your overriding .tpl.php file

<?php print "<pre>"; print_r(array_values($form));print "</pre>"; ?>

and it'll output a complete list of things you can theme.
Not ground breaking I know, but one of those things I can never seem to find when I want!

6 comments

1
soxofaanOctober 12th 2007 @ 03:47PM

with the devel module (http://drupal.org/project/devel) enabled it can be even as easy as "dvm($form)"

2
Nate HaugOctober 12th 2007 @ 07:45PM

Or if you want to take it further and get ALL variables available within any of your .tpl.php file:

<pre><?php print_r(get_defined_vars()); ?></pre>

3
LaurenceOctober 12th 2007 @ 10:48PM

Thanks guys, excellent suggestions!

4
ppOctober 13th 2007 @ 03:14PM

I use this:

<?php
drupal_set_message
('<pre>'.print_r(array_values($form),true).'</pre>');
?>

5
GuestJuly 23rd 2008 @ 09:17PM

var_dump ;)

6
RickDecember 9th 2008 @ 07:54PM

This is how I modify forms in Drupal 5 using phptemplate. I haven't needed to in 6 yet. Probably similar.

function zen_node_form($form)
{
        //all node edit forms
$form['path']['path']['#description'] .= 'my custom additions';
        //specific node types
switch($form['form_id']['#value'])
{
case "video_node_form":
return _phptemplate_callback('video_edit', array('form' => $form), array('video-edit'));
break;
default:
  return drupal_render($form);
}
}

and video-edit.tpl.php (the template you registered in _phptempleat_callback()) contains:
$form['attachments']['#weight'] = -4;
$form['field_screenshot']['#weight'] = -3;
$form['field_duration']['#weight'] = -2;
$form['attachments']['#title'] = "Video";
$form['attachments']['#collapsable'] = 1;
$form['attachments']['#collapsed'] = 0;
print drupal_render($form);