Commit 2d1ef531 authored by webchick's avatar webchick

#601806 by sun, effulgentsia, and Damien Tournoud: drupal_render() should not...

#601806 by sun, effulgentsia, and Damien Tournoud: drupal_render() should not set default element properties that make no sense.
parent 9db02aba
...@@ -4939,9 +4939,8 @@ function drupal_render_page($page) { ...@@ -4939,9 +4939,8 @@ function drupal_render_page($page) {
* The rendered HTML. * The rendered HTML.
*/ */
function drupal_render(&$elements) { function drupal_render(&$elements) {
static $defaults;
// Early-return nothing if user does not have access. // 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; return;
} }
...@@ -4954,7 +4953,7 @@ function drupal_render(&$elements) { ...@@ -4954,7 +4953,7 @@ function drupal_render(&$elements) {
if (isset($elements['#cache']) && $cached_output = drupal_render_cache_get($elements)) { if (isset($elements['#cache']) && $cached_output = drupal_render_cache_get($elements)) {
return $cached_output; return $cached_output;
} }
// If #markup is not empty, set #type. This allows to specify just #markup on // If #markup is not empty, set #type. This allows to specify just #markup on
// an element without setting #type. // an element without setting #type.
if (!empty($elements['#markup']) && !isset($elements['#type'])) { if (!empty($elements['#markup']) && !isset($elements['#type'])) {
...@@ -4966,12 +4965,6 @@ function drupal_render(&$elements) { ...@@ -4966,12 +4965,6 @@ function drupal_render(&$elements) {
if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) { if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
$elements += element_info($elements['#type']); $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 // 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 // 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) { ...@@ -5062,7 +5055,9 @@ function drupal_render_children(&$element, $children_keys = NULL) {
} }
$output = ''; $output = '';
foreach ($children_keys as $key) { foreach ($children_keys as $key) {
$output .= drupal_render($element[$key]); if (!empty($element[$key])) {
$output .= drupal_render($element[$key]);
}
} }
return $output; return $output;
} }
...@@ -5166,7 +5161,9 @@ function drupal_render_cache_set($markup, $elements) { ...@@ -5166,7 +5161,9 @@ function drupal_render_cache_set($markup, $elements) {
$data['#markup'] = $markup; $data['#markup'] = $markup;
// Persist attached data associated with this element. // 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'; $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
$expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT; $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT;
cache_set($cid, $data, $bin, $expire); cache_set($cid, $data, $bin, $expire);
...@@ -5258,10 +5255,8 @@ function element_info($type) { ...@@ -5258,10 +5255,8 @@ function element_info($type) {
$cache = &drupal_static(__FUNCTION__); $cache = &drupal_static(__FUNCTION__);
if (!isset($cache)) { if (!isset($cache)) {
$basic_defaults = element_basic_defaults();
$cache = module_invoke_all('element_info'); $cache = module_invoke_all('element_info');
foreach ($cache as $element_type => $info) { foreach ($cache as $element_type => $info) {
$cache[$element_type] = array_merge_recursive($basic_defaults, $info);
$cache[$element_type]['#type'] = $element_type; $cache[$element_type]['#type'] = $element_type;
} }
// Allow modules to alter the element type defaults. // Allow modules to alter the element type defaults.
...@@ -5271,19 +5266,6 @@ function element_info($type) { ...@@ -5271,19 +5266,6 @@ function element_info($type) {
return isset($cache[$type]) ? $cache[$type] : array(); 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. * Function used by uasort to sort structured arrays by weight, without the property weight prefix.
*/ */
......
...@@ -1017,11 +1017,16 @@ function form_builder($form_id, $element, &$form_state) { ...@@ -1017,11 +1017,16 @@ function form_builder($form_id, $element, &$form_state) {
$element['#processed'] = FALSE; $element['#processed'] = FALSE;
// Use element defaults. // 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. // Overlay $info onto $element, retaining preexisting keys in $element.
$element += $info; $element += $info;
$element['#defaults_loaded'] = TRUE; $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. // Special handling if we're on the top level form element.
if (isset($element['#type']) && $element['#type'] == 'form') { if (isset($element['#type']) && $element['#type'] == 'form') {
...@@ -1739,7 +1744,20 @@ function form_get_options($element, $key) { ...@@ -1739,7 +1744,20 @@ function form_get_options($element, $key) {
*/ */
function theme_fieldset($variables) { function theme_fieldset($variables) {
$element = $variables['element']; $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) { ...@@ -1765,7 +1783,8 @@ function theme_radio($variables) {
$output .= 'value="' . $element['#return_value'] . '" '; $output .= 'value="' . $element['#return_value'] . '" ';
$output .= (check_plain($element['#value']) == $element['#return_value']) ? ' checked="checked" ' : ' '; $output .= (check_plain($element['#value']) == $element['#return_value']) ? ' checked="checked" ' : ' ';
$output .= drupal_attributes($element['#attributes']) . ' />'; $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>'; $output = '<label class="option" for="' . $element['#id'] . '">' . $output . ' ' . $element['#title'] . '</label>';
} }
...@@ -2119,7 +2138,7 @@ function theme_checkbox($variables) { ...@@ -2119,7 +2138,7 @@ function theme_checkbox($variables) {
} }
$checkbox .= drupal_attributes($element['#attributes']) . ' />'; $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>' : ''; $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>'; $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . $required . '</label>';
} }
...@@ -2157,7 +2176,7 @@ function theme_checkboxes($variables) { ...@@ -2157,7 +2176,7 @@ function theme_checkboxes($variables) {
* This is used as a pre render function for checkboxes and radios. * This is used as a pre render function for checkboxes and radios.
*/ */
function form_pre_render_conditional_form_element($element) { function form_pre_render_conditional_form_element($element) {
if ($element['#title'] || $element['#description']) { if (isset($element['#title']) || isset($element['#description'])) {
unset($element['#id']); unset($element['#id']);
$element['#theme_wrappers'][] = 'form_element'; $element['#theme_wrappers'][] = 'form_element';
} }
......
...@@ -488,7 +488,7 @@ function system_element_info() { ...@@ -488,7 +488,7 @@ function system_element_info() {
$types['token'] = array( $types['token'] = array(
'#input' => TRUE, '#input' => TRUE,
'#theme' => array('hidden'), '#theme' => 'hidden',
); );
return $types; return $types;
......
...@@ -77,5 +77,18 @@ function seven_tablesort_indicator($variables) { ...@@ -77,5 +77,18 @@ function seven_tablesort_indicator($variables) {
*/ */
function seven_fieldset($variables) { function seven_fieldset($variables) {
$element = $variables['element']; $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;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment