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);

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

4 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.