From 2d1ef531dcca714d60bd2f61927d85a9b77d4957 Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Mon, 16 Nov 2009 05:11:01 +0000
Subject: [PATCH] #601806 by sun, effulgentsia, and Damien Tournoud:
 drupal_render() should not set default element properties that make no sense.

---
 includes/common.inc          | 34 ++++++++--------------------------
 includes/form.inc            | 29 ++++++++++++++++++++++++-----
 modules/system/system.module |  2 +-
 themes/seven/template.php    | 15 ++++++++++++++-
 4 files changed, 47 insertions(+), 33 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index 729e67652386..7adc5bc470a1 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -4939,9 +4939,8 @@ function drupal_render_page($page) {
  *   The rendered HTML.
  */
 function drupal_render(&$elements) {
-  static $defaults;
   // Early-return nothing if user does not have access.
-  if (!isset($elements) || (isset($elements['#access']) && !$elements['#access'])) {
+  if (empty($elements) || (isset($elements['#access']) && !$elements['#access'])) {
     return;
   }
 
@@ -4954,7 +4953,7 @@ function drupal_render(&$elements) {
   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'])) {
@@ -4966,12 +4965,6 @@ function drupal_render(&$elements) {
   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
@@ -5062,7 +5055,9 @@ function drupal_render_children(&$element, $children_keys = NULL) {
   }
   $output = '';
   foreach ($children_keys as $key) {
-    $output .= drupal_render($element[$key]);
+    if (!empty($element[$key])) {
+      $output .= drupal_render($element[$key]);
+    }
   }
   return $output;
 }
@@ -5166,7 +5161,9 @@ function drupal_render_cache_set($markup, $elements) {
 
   $data['#markup'] = $markup;
   // Persist attached data associated with this element.
-  $data['#attached'] = $elements['#attached'];
+  if (isset($elements['#attached'])) {
+    $data['#attached'] = $elements['#attached'];
+  }
   $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
   $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT;
   cache_set($cid, $data, $bin, $expire);
@@ -5258,10 +5255,8 @@ function element_info($type) {
   $cache = &drupal_static(__FUNCTION__);
 
   if (!isset($cache)) {
-    $basic_defaults = element_basic_defaults();
     $cache = module_invoke_all('element_info');
     foreach ($cache as $element_type => $info) {
-      $cache[$element_type] = array_merge_recursive($basic_defaults, $info);
       $cache[$element_type]['#type'] = $element_type;
     }
     // Allow modules to alter the element type defaults.
@@ -5271,19 +5266,6 @@ function element_info($type) {
   return isset($cache[$type]) ? $cache[$type] : array();
 }
 
-/**
- * Retrieve the basic default properties that are common to all elements.
- */
-function element_basic_defaults() {
-  return array(
-    '#description' => '',
-    '#title' => '',
-    '#attributes' => array(),
-    '#required' => FALSE,
-    '#attached' => array(),
-  );
-}
-
 /**
  * Function used by uasort to sort structured arrays by weight, without the property weight prefix.
  */
diff --git a/includes/form.inc b/includes/form.inc
index 6f7636c952e3..f5ce76b5f637 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -1017,11 +1017,16 @@ function form_builder($form_id, $element, &$form_state) {
   $element['#processed'] = FALSE;
 
   // Use element defaults.
-  if ((!empty($element['#type'])) && ($info = element_info($element['#type']))) {
+  if (isset($element['#type']) && ($info = element_info($element['#type']))) {
     // Overlay $info onto $element, retaining preexisting keys in $element.
     $element += $info;
     $element['#defaults_loaded'] = TRUE;
   }
+  // Assign basic defaults common for all form elements.
+  $element += array(
+    '#required' => FALSE,
+    '#attributes' => array(),
+  );
 
   // Special handling if we're on the top level form element.
   if (isset($element['#type']) && $element['#type'] == 'form') {
@@ -1739,7 +1744,20 @@ function form_get_options($element, $key) {
  */
 function theme_fieldset($variables) {
   $element = $variables['element'];
-  return '<fieldset' . drupal_attributes($element['#attributes']) . '>' . ($element['#title'] ? '<legend>' . $element['#title'] . '</legend>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="fieldset-description">' . $element['#description'] . '</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . (isset($element['#value']) ? $element['#value'] : '') . "</fieldset>\n";
+
+  $output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
+  if (!empty($element['#title'])) {
+    $output .= '<legend>' . $element['#title'] . '</legend>';
+  }
+  if (!empty($element['#description'])) {
+    $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
+  }
+  $output .= $element['#children'];
+  if (isset($element['#value'])) {
+    $output .= $element['#value'];
+  }
+  $output .= "</fieldset>\n";
+  return $output;
 }
 
 /**
@@ -1765,7 +1783,8 @@ function theme_radio($variables) {
   $output .= 'value="' . $element['#return_value'] . '" ';
   $output .= (check_plain($element['#value']) == $element['#return_value']) ? ' checked="checked" ' : ' ';
   $output .= drupal_attributes($element['#attributes']) . ' />';
-  if (!is_null($element['#title'])) {
+
+  if (isset($element['#title'])) {
     $output = '<label class="option" for="' . $element['#id'] . '">' . $output . ' ' . $element['#title'] . '</label>';
   }
 
@@ -2119,7 +2138,7 @@ function theme_checkbox($variables) {
   }
   $checkbox .= drupal_attributes($element['#attributes']) . ' />';
 
-  if (!is_null($element['#title'])) {
+  if (isset($element['#title'])) {
     $required = !empty($element['#required']) ? ' <span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';
     $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . $required . '</label>';
   }
@@ -2157,7 +2176,7 @@ function theme_checkboxes($variables) {
  * This is used as a pre render function for checkboxes and radios.
  */
 function form_pre_render_conditional_form_element($element) {
-  if ($element['#title'] || $element['#description']) {
+  if (isset($element['#title']) || isset($element['#description'])) {
     unset($element['#id']);
     $element['#theme_wrappers'][] = 'form_element';
   }
diff --git a/modules/system/system.module b/modules/system/system.module
index 228ffd050406..7385b8d939d1 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -488,7 +488,7 @@ function system_element_info() {
 
   $types['token'] = array(
     '#input' => TRUE,
-    '#theme' => array('hidden'),
+    '#theme' => 'hidden',
   );
 
   return $types;
diff --git a/themes/seven/template.php b/themes/seven/template.php
index 856beeb38408..b3a347bae6ce 100644
--- a/themes/seven/template.php
+++ b/themes/seven/template.php
@@ -77,5 +77,18 @@ function seven_tablesort_indicator($variables) {
  */
 function seven_fieldset($variables) {
   $element = $variables['element'];
-  return '<fieldset' . drupal_attributes($element['#attributes']) . '>' . ($element['#title'] ? '<legend><span>' . $element['#title'] . '</span></legend>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="fieldset-description">' . $element['#description'] . '</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . (isset($element['#value']) ? $element['#value'] : '') . "</fieldset>\n";
+
+  $output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
+  if (!empty($element['#title'])) {
+    $output .= '<legend><span>' . $element['#title'] . '</span></legend>';
+  }
+  if (!empty($element['#description'])) {
+    $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
+  }
+  $output .= $element['#children'];
+  if (isset($element['#value'])) {
+    $output .= $element['#value'];
+  }
+  $output .= "</fieldset>\n";
+  return $output;
 }
-- 
GitLab