drupal_render

Versions
5 – 7
drupal_render(&$elements)

Renders HTML given a structured array tree.

Recursively iterates over each of the array elements, generating HTML code.

HTML generation is controlled by two properties containing theme functions, #theme and #theme_wrappers.

#theme is the theme function called first. If it is set and the element has any children, they have to be rendered there. For elements that are not allowed to have any children, e.g. buttons or textfields, it can be used to render the element itself. If #theme is not present and the element has children, they are rendered and concatenated into a string by drupal_render_children().

The #theme_wrappers property contains an array of theme functions which will be called, in order, after #theme has run. These can be used to add further markup around the rendered children; e.g., fieldsets add the required markup for a fieldset around their rendered child elements. All wrapper theme functions have to include the element's #children property in their output, as it contains the output of the previous theme functions and the rendered children.

For example, for the form element type, by default only the #theme_wrappers property is set, which adds the form markup around the rendered child elements of the form. This allows you to set the #theme property on a specific form to a custom theme function, giving you complete control over the placement of the form's children while not at all having to deal with the form markup itself.

drupal_render() can optionally cache the rendered output of elements to improve performance. To use drupal_render() caching, set the element's #cache property to an associative array with one or several of the following keys:

  • 'keys': An array of one or more keys that identify the element. If 'keys' is set, the cache ID is created automatically from these keys. @see drupal_render_cid_create()
  • 'granularity' (optional): Define the cache granularity using binary combinations of the cache granularity constants, e.g. DRUPAL_CACHE_PER_USER to cache for each user seperately or DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE to cache seperately for each page and role. If not specified the element is cached globally for each theme and language.
  • 'cid': Specify the cache ID directly. Either 'keys' or 'cid' is required. If 'cid' is set, 'keys' and 'granularity' are ignored. Use only if you have special requirements.
  • 'expire': Set to one of the cache lifetime constants.
  • 'bin': Specify a cache bin to cache the element in. Defaults to 'cache'.

This function is usually called from within another function, like drupal_get_form() or a theme function. Elements are sorted internally using uasort(). Since this is expensive, when passing already sorted elements to drupal_render(), for example from a database query, set $elements['#sorted'] = TRUE to avoid sorting them a second time.

Parameters

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

Return value

The rendered HTML.

▾ 69 functions call drupal_render()

book_children in modules/book/book.module
Format the menu links for the child pages of the current page.
book_form_update in modules/book/book.pages.inc
AJAX callback to replace the book parent select options.
book_node_export in modules/book/book.module
Generates printer-friendly HTML for a node.
dashboard_show_block_content in modules/dashboard/dashboard.module
AJAX callback to display the rendered contents of a specific block.
drupal_get_html_head in includes/common.inc
Retrieve output to be displayed in the HEAD tag of the HTML page.
drupal_render in includes/common.inc
Renders HTML given a structured array tree.
drupal_render_children in includes/common.inc
Render children of an element and concatenate them.
drupal_render_page in includes/common.inc
Renders the page, including all theming.
field_add_more_js in modules/field/field.form.inc
Ajax callback for addition of new empty widgets.
file_ajax_upload in modules/file/file.module
Menu callback; Shared AJAX callback for file uploads and deletions.
hook_search_execute in modules/search/search.api.php
Execute a search for a set of key words.
hook_update_index in modules/search/search.api.php
Update Drupal's full-text index for this module.
locale_translate_export_screen in includes/locale.inc
User interface for the translation export screen.
locale_translate_seek_screen in includes/locale.inc
String search screen.
node_feed in modules/node/node.module
A generic function for generating RSS feeds from a set of nodes.
node_search_execute in modules/node/node.module
Implement hook_search_execute().
node_unpublish_by_keyword_action in modules/node/node.module
Implement a configurable Drupal action. Unpublish a node if it contains a certain string.
openid_redirect in modules/openid/openid.inc
Creates a js auto-submit redirect for (for the 2.x protocol)
render in includes/common.inc
Render and print an element.
shortcut_preprocess_page in modules/shortcut/shortcut.module
Implement hook_preprocess_page().
template_preprocess_block_admin_display_form in modules/block/block.admin.inc
Process variables for block-admin-display.tpl.php.
template_preprocess_field_ui_display_overview_form in modules/field_ui/field_ui.admin.inc
Theme preprocess function for field_ui-display-overview-form.tpl.php.
template_preprocess_field_ui_field_overview_form in modules/field_ui/field_ui.admin.inc
Theme preprocess function for field_ui-field-overview-form.tpl.php.
template_preprocess_poll_results in modules/poll/poll.module
Preprocess the poll_results theme hook.
template_preprocess_poll_vote in modules/poll/poll.module
Themes the voting form for a poll.
template_preprocess_search_block_form in modules/search/search.module
Process variables for search-block-form.tpl.php.
template_process_html in includes/theme.inc
Process variables for html.tpl.php
theme_aggregator_categorize_items in modules/aggregator/aggregator.pages.inc
Theme the page list form for assigning categories.
theme_book_admin_table in modules/book/book.admin.inc
Theme function for the book administration page form.
theme_dblog_filters in modules/dblog/dblog.module
Theme dblog administration filter selector.
theme_field_multiple_value_form in modules/field/field.form.inc
Theme an individual form element.
theme_file_widget_multiple in modules/file/file.field.inc
Theme a group of file upload widgets.
theme_filter_admin_order in modules/filter/filter.admin.inc
Theme filter order configuration form.
theme_filter_admin_overview in modules/filter/filter.admin.inc
Theme the text format administration overview form.
theme_image_anchor in modules/image/image.admin.inc
Theme callback for displaying a grid of checkboxes.
theme_image_style_effects in modules/image/image.admin.inc
Theme callback for listing the effects within a specific image style.
theme_image_widget in modules/image/image.field.inc
Theme the display of the image field widget.
theme_locale_date_format_form in modules/locale/locale.module
Theme locale date format form.
theme_locale_languages_configure_form in includes/locale.inc
Theme the language configure form.
theme_locale_languages_overview_form in includes/locale.inc
Theme the language overview form.
theme_locale_translation_filters in modules/locale/locale.module
Theme locale translation filter selector.
theme_menu_link in includes/menu.inc
Generate the HTML output for a menu link and submenu.
theme_node_filters in modules/node/node.admin.inc
Theme node administration filter selector.
theme_node_filter_form in modules/node/node.admin.inc
Theme node administration filter form.
theme_node_preview in modules/node/node.pages.inc
Display a node preview for display during node creation and editing.
theme_node_search_admin in modules/node/node.module
Theme the content ranking part of the search settings admin page.
theme_poll_choices in modules/poll/poll.module
Theme the admin poll form for choices.
theme_profile_admin_overview in modules/profile/profile.admin.inc
Theme the profile field overview into a drag and drop enabled table.
theme_scaffolding_example_overview_form in developer/examples/scaffolding_example/scaffolding_example.admin.inc
Theme the drag-and-drop overview form.
theme_shortcut_set_customize in modules/shortcut/shortcut.admin.inc
Theme function for the shortcut set customization form.
theme_shortcut_set_switch in modules/shortcut/shortcut.admin.inc
Theme function for the form that switches shortcut sets.
theme_simpletest_test_table in modules/simpletest/simpletest.pages.inc
Theme the test list generated by simpletest_test_form() into a table.
theme_system_date_time_settings in modules/system/system.admin.inc
Theme function for date settings form.
theme_system_modules_fieldset in modules/system/system.admin.inc
Theme callback for the modules form.
theme_system_modules_uninstall in modules/system/system.admin.inc
Themes a table of currently disabled modules.
theme_system_themes_form in modules/system/system.admin.inc
Theme function for the system themes form.
theme_tableselect in includes/form.inc
Format a table with radio buttons or checkboxes.
theme_taxonomy_overview_terms in modules/taxonomy/taxonomy.admin.inc
Theme the terms overview as a sortable list of terms.
theme_taxonomy_overview_vocabularies in modules/taxonomy/taxonomy.admin.inc
Theme the vocabulary overview as a sortable list of vocabularies.
theme_upload_form_current in modules/upload/upload.module
Theme the attachments list.
theme_user_admin_new_role in modules/user/user.admin.inc
Theme the new-role form.
theme_user_admin_permissions in modules/user/user.admin.inc
Theme the administer permissions page.
theme_user_filters in modules/user/user.admin.inc
Theme user administration filter selector.
theme_user_filter_form in modules/user/user.admin.inc
Theme user administration filter form.
theme_user_profile in developer/theme.php
Theme a user page.
upload_js in modules/upload/upload.module
Menu-callback for JavaScript-based uploads.
_field_ui_overview_order in modules/field_ui/field_ui.admin.inc
Helper function to order fields when theming overview forms.
_node_index_node in modules/node/node.module
Index a single node.
_theme_table_cell in includes/theme.inc

Code

includes/common.inc, line 4906

<?php
function drupal_render(&$elements) {
  static $defaults;
  // Early-return nothing if user does not have access.
  if (!isset($elements) || (isset($elements['#access']) && !$elements['#access'])) {
    return;
  }

  // Do not print elements twice.
  if (isset($elements['#printed']) && $elements['#printed']) {
    return;
  }

  // Try to fetch the element's markup from cache and return.
  if (isset($elements['#cache']) && $cached_output = drupal_render_cache_get($elements)) {
    return $cached_output;
  }
  
  // If #markup is not empty, set #type. This allows to specify just #markup on
  // an element without setting #type.
  if (!empty($elements['#markup']) && !isset($elements['#type'])) {
    $elements['#type'] = 'markup';
  }

  // If the default values for this element have not been loaded yet, populate
  // them.
  if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
    $elements += element_info($elements['#type']);
  }
  else {
    if (!isset($defaults)) {
      $defaults = element_basic_defaults();
    }
    $elements += $defaults;
  }

  // Make any final changes to the element before it is rendered. This means
  // that the $element or the children can be altered or corrected before the
  // element is rendered into the final text.
  if (isset($elements['#pre_render'])) {
    foreach ($elements['#pre_render'] as $function) {
      if (function_exists($function)) {
        $elements = $function($elements);
      }
    }
  }

  // Get the children of the element, sorted by weight.
  $children = element_children($elements, TRUE);

  // Initialize this element's #children, unless a #pre_render callback already
  // preset #children.
  if (!isset($elements['#children'])) {
    $elements['#children'] = '';
  }
  // Call the element's #theme function if it is set. Then any children of the
  // element have to be rendered there.
  if (isset($elements['#theme'])) {
    $elements['#children'] = theme($elements['#theme'], $elements);
  }
  // If #theme was not set and the element has children, render them now.
  // This is the same process as drupal_render_children() but is inlined
  // for speed.
  if ($elements['#children'] == '') {
    foreach ($children as $key) {
      $elements['#children'] .= drupal_render($elements[$key]);
    }
  }

  // Let the theme functions in #theme_wrappers add markup around the rendered
  // children.
  if (isset($elements['#theme_wrappers'])) {
    foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
      $elements['#children'] = theme($theme_wrapper, $elements);
    }
  }

  // Filter the outputted content and make any last changes before the
  // content is sent to the browser. The changes are made on $content
  // which allows the output'ed text to be filtered.
  if (isset($elements['#post_render'])) {
    foreach ($elements['#post_render'] as $function) {
      if (function_exists($function)) {
        $elements['#children'] = $function($elements['#children'], $elements);
      }
    }
  }

  // Add any JavaScript state information associated with the element.
  drupal_process_states($elements);

  // Add additional libraries, CSS, JavaScript an other custom
  // attached data associated with this element.
  drupal_process_attached($elements);

  $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
  $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';

  // Cache the processed element if #cache is set.
  if (isset($elements['#cache'])) {
    drupal_render_cache_set($prefix . $elements['#children'] . $suffix, $elements);
  }

  $elements['#printed'] = TRUE;
  return $prefix . $elements['#children'] . $suffix;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.