I've got a simple form which I would like to run through a theme function of mine. What I do is that I've got a form builder function where I define the form. I've then used hook_theme() to define which theme function Drupal should use to theme the form.

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_form' => array(
      'render element' => 'form',

Drupal does invoke the theme function. However, I've got a call to drupal_render($variables['form']) to actually render the form in my theme function. What happens then is that Drupal seems to go into a loop as this call results in mymodule_theme to be called again (and again, and again...).

function theme_mymodule_form($variables){

  // Add custom text on top.
  $output = '<span>' . t('Table description goes here') . '</span>';
  $output .= drupal_render($variables['form']);

  // Return output.
  return $output;


When I do a dump of $variables passed to my theme function, I see that #theme is set to 'mymodule_theme'. Should I at this point change it to 'form', and not 'mymodule_form' as I'd just like to render the form as a plain form? How should I really render the $variables'[form'] in my theme function to make it appear as a plain old form? Should I rather use theme('form', $variables['form']) for that?

As far as I understand, drupal_render($variables['form']) takes care of rendering the form and all its children, as opposed to to theme('form', $variables['form']) which only renders the immediate form and not children.

I'd be really happy if someone have the time to enlighten me on this :-)


In Drupal 7 you need to use drupal_render_children() instead of drupal_render() for the $form. Otherwise, if the form has the #theme property set you will get an infinite loop (as your theme function will invoke the form builder to run your theme function again, and the cycle will continue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.