You are here

function drupal_render in Drupal 5

Same name and namespace in other branches
  1. 8 core/includes/common.inc \drupal_render()
  2. 6 includes/common.inc \drupal_render()
  3. 7 includes/common.inc \drupal_render()

Renders HTML given a structured array tree. Recursively iterates over each of the array elements, generating HTML code. This function is usually called from within a another function, like drupal_get_form() or node_view().

Parameters

$elements: The structured array describing the data to be rendered.

Return value

The rendered HTML.

Related topics

36 calls to drupal_render()
book_node_visitor_html_pre in modules/book/book.module
Generates printer-friendly HTML for a node. This function is a 'pre-node' visitor function for book_recurse().
drupal_render_form in includes/form.inc
Renders a structured form array into themed HTML.
node_search in modules/node/node.module
Implementation of hook_search().
node_update_index in modules/node/node.module
Implementation of hook_update_index().
node_view in modules/node/node.module
Generate a display of the given node.

... See full list

File

includes/common.inc, line 2277
Common functions that many Drupal modules will need to reference.

Code

function drupal_render(&$elements) {
  if (!isset($elements) || isset($elements['#access']) && !$elements['#access']) {
    return NULL;
  }
  $content = '';

  // Either the elements did not go through form_builder or one of the children
  // has a #weight.
  if (!isset($elements['#sorted'])) {
    uasort($elements, "_element_sort");
  }
  if (!isset($elements['#children'])) {
    $children = element_children($elements);

    /* Render all the children that use a theme function */
    if (isset($elements['#theme']) && empty($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 drupal_render and concatenate them */
    if (!isset($content) || $content === '') {
      foreach ($children as $key) {
        $content .= drupal_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(!empty($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;
  }
}