form_render

Definition

form_render(&$elements)
includes/form.inc, line 523

Description

Renders a HTML form given a form tree. Recursively iterates over each of the form elements, generating HTML code. This function is usually called from within a theme. To render a form from within a module, use drupal_get_form().

Parameters

$elements The form tree describing the form.

Return value

The rendered HTML form.

Related topics

Namesort iconDescription
Form generationFunctions to enable output of HTML forms and form elements.

Code

<?php
function form_render(&$elements) {
  if (!isset($elements)) {
    return NULL;
  }
  $content = '';
  uasort($elements, "_form_sort");
  if (!isset($elements['#children'])) {
    $children = element_children($elements);
    /* Render all the children that use a theme function */
    if (isset($elements['#theme']) && !$elements['#theme_used']) {
      $elements['#theme_used'] = TRUE;

      $previous = array();
      foreach (array('#value', '#type', '#prefix', '#suffix') as $key) {
        $previous[$key] = isset($elements[$key]) ? $elements[$key] : NULL;
      }
      // If we rendered a single element, then we will skip the renderer.
      if (empty($children)) {
        $elements['#printed'] = TRUE;
      }
      else {
        $elements['#value'] = '';
      }
      $elements['#type'] = 'markup';

      unset($elements['#prefix'], $elements['#suffix']);
      $content = theme($elements['#theme'], $elements);

      foreach (array('#value', '#type', '#prefix', '#suffix') as $key) {
        $elements[$key] = isset($previous[$key]) ? $previous[$key] : NULL;
      }
    }
    /* render each of the children using form_render and concatenate them */
    if (!isset($content) || $content === '') {
      foreach ($children as $key) {
        $content .= form_render($elements[$key]);
      }
    }
  }
  if (isset($content) && $content !== '') {
    $elements['#children'] = $content;
  }

  // Until now, we rendered the children, here we render the element itself
  if (!isset($elements['#printed'])) {
    $content = theme(($elements['#type']) ? $elements['#type']: 'markup', $elements);
    $elements['#printed'] = TRUE;
  }

  if (isset($content) && $content !== '') {
    $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
    $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
    return $prefix . $content . $suffix;
  }
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.