4.7.x form.inc theme_form($element)
5.x form.inc theme_form($element)
6.x form.inc theme_form($element)
7.x form.inc theme_form($variables)

Returns HTML for a form.


$variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #action, #method, #attributes, #children

Related topics


includes/form.inc, line 4046
Functions for form and batch generation and processing.


function theme_form($variables) {
  $element = $variables['element'];
  if (isset($element['#action'])) {
    $element['#attributes']['action'] = drupal_strip_dangerous_protocols($element['#action']);
  element_set_attributes($element, array(
  if (empty($element['#attributes']['accept-charset'])) {
    $element['#attributes']['accept-charset'] = "UTF-8";

  // Anonymous DIV to satisfy XHTML compliance.
  return '<form' . drupal_attributes($element['#attributes']) . '><div>' . $element['#children'] . '</div></form>';


uesnet’s picture

I am trying to theme a form, the template for my theme is applied correctly but the form html tag, is missing. Instead all elements are wrapped in tags. This causes my submit button to not do anything. If I remove the theme, everything works correctly. I have the same result using the theme('mytheme', $form) and setting the #theme attribute within the form. Not sure what else to try. Thanks in advance for any help.

aimsson’s picture

I had the same problem and solved it this way:

function your_theme_function($form) {
  $variables['element'] = $form;
  // render all form elements and/or add custom markup. ** Addition: don't forget to render the hidden inputs form_id and form_build_id, otherwise your form will not work at all
  $output = '
'; $output .= drupal_render($form['elem2']); ... $output .= drupal_render($form['form_build_id']); $output .= drupal_render($form['form_id']); // you can also use $output .= drupal_render($form) to print all form elements that have left after manual rendering $variables['element']['#children'] = $output; return theme_form($variables); }

then just simply use

print your_theme_function(drupal_get_form('your_form'));

in the place you want to output your modified form - that's all. this way worked for me....

tmk1’s picture

in my case:

function tcontrol_start_theme($form) {
  $variables['element'] = $form;
  $output = drupal_render($form);
  $variables['element']['#children'] = $output;
  return theme_form($variables);


$output .= tcontrol_start_theme(drupal_get_form('tcontrol_start_form'));

as a part of a longer output

dmaciej’s picture

it gives me "Maximum function nesting level of '100' reached, aborting!"

BrianLewisDesign’s picture

In your form theme:
drupal_render() your #children, then theme_form().

$buildform = drupal_render($form['element1']);
$buildform .= drupal_render($form['element2']);
$buildform .= drupal_render($form['form_build_id']);
$buildform .= drupal_render($form['form_id']);
$buildform .= drupal_render($form['form_token']);
$form['element']['#children'] = $buildform;
$output = theme_form($form);
phponwebsites’s picture

How to avoid 'div' tag followed by 'form' tag while rendering drupal form?

Loque’s picture

Just incase you are still running into this (or anyone else), I found the following two links useful


If possible I would recommend ensuring all markup is maintained in .tpl files - there is a time overhead involved in this, if you will be building lots of Drupal sites I would definitely recommend it!