Same name and namespace in other branches
- 4.7.x includes/form.inc \element_children()
- 5.x includes/common.inc \element_children()
- 6.x includes/common.inc \element_children()
Identifies the children of an element array, optionally sorted by weight.
The children of a element array are those key/value pairs whose key does not start with a '#'. See drupal_render() for details.
Parameters
$elements: The element array whose children are to be identified.
$sort: Boolean to indicate whether the children should be sorted by weight.
Return value
The array keys of the element's children.
55 calls to element_children()
- DrupalRenderTestCase::testDrupalRenderSorting in modules/
simpletest/ tests/ common.test - Test sorting by weight.
- drupal_pre_render_links in includes/
common.inc - #pre_render callback that collects child links into a single array.
- drupal_pre_render_scripts in includes/
common.inc - The #pre_render callback for the "scripts" element.
- drupal_render in includes/
common.inc - Renders HTML given a structured array tree.
- drupal_render_children in includes/
common.inc - Renders children of an element and concatenates them.
File
- includes/
common.inc, line 6718 - Common functions that many Drupal modules will need to reference.
Code
function element_children(&$elements, $sort = FALSE) {
// Do not attempt to sort elements which have already been sorted.
$sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
// Filter out properties from the element, leaving only children.
$count = count($elements);
$child_weights = array();
$i = 0;
$sortable = FALSE;
foreach ($elements as $key => $value) {
if (is_int($key) || $key === '' || $key[0] !== '#') {
if (is_array($value) && isset($value['#weight'])) {
$weight = $value['#weight'];
$sortable = TRUE;
}
else {
$weight = 0;
}
// Support weights with up to three digit precision and conserve the
// insertion order.
$child_weights[$key] = floor($weight * 1000) + $i / $count;
}
$i++;
}
// Sort the children if necessary.
if ($sort && $sortable) {
asort($child_weights);
// 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 ($child_weights as $key => $weight) {
$value = $elements[$key];
unset($elements[$key]);
$elements[$key] = $value;
}
$elements['#sorted'] = TRUE;
}
return array_keys($child_weights);
}