Commit b83aa19b authored by webchick's avatar webchick
Browse files

#370846 follow-up by catch: Fix #weight sorting in drupal_render().

parent 826cf54d
...@@ -3493,11 +3493,19 @@ function element_children(&$elements, $sort = FALSE) { ...@@ -3493,11 +3493,19 @@ function element_children(&$elements, $sort = FALSE) {
} }
} }
} }
// Sort the element if necessary. // Sort the children if necessary.
if ($sort && $sortable) { if ($sort && $sortable) {
uasort($children, 'element_sort'); uasort($children, 'element_sort');
// Put the sorted children back into $elements in the correct order, to
// preserve sorting if the same element is passed through
// element_children() twice.
foreach ($children as $key => $child) {
unset($elements[$key]);
$elements[$key] = $child;
}
$elements['#sorted'] = TRUE;
} }
$elements['#sorted'] = TRUE;
return array_keys($children); return array_keys($children);
} }
......
...@@ -542,7 +542,15 @@ class DrupalRenderUnitTestCase extends DrupalWebTestCase { ...@@ -542,7 +542,15 @@ class DrupalRenderUnitTestCase extends DrupalWebTestCase {
// Confirm that the $elements array has '#sorted' set to TRUE. // Confirm that the $elements array has '#sorted' set to TRUE.
$this->assertTrue($elements['#sorted'], t("'#sorted' => TRUE was added to the array")); $this->assertTrue($elements['#sorted'], t("'#sorted' => TRUE was added to the array"));
// Now the same array structure, but with #sorted set to TRUE. // Pass $elements through element_children() and ensure it remains
// sorted in the correct order. drupal_render() will return an empty string
// if used on the same array in the same request.
$children = element_children($elements);
$this->assertTrue(array_shift($children) == 'first', t('Child found in the correct order.'));
$this->assertTrue(array_shift($children) == 'second', t('Child found in the correct order.'));
// The same array structure again, but with #sorted set to TRUE.
$elements = array( $elements = array(
'second' => array( 'second' => array(
'#weight' => 10, '#weight' => 10,
......
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