function element_children in Drupal 7
Same name and namespace in other branches
- 4 includes/form.inc \element_children()
- 5 includes/common.inc \element_children()
- 6 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.
69 calls to element_children()
- book_admin_edit_submit in modules/
book/ book.admin.inc - Form submission handler for book_admin_edit().
- dashboard_form_block_admin_configure_alter in modules/
dashboard/ dashboard.module - Implements hook_form_FORM_ID_alter().
- dashboard_form_block_admin_display_form_alter in modules/
dashboard/ dashboard.module - Implements hook_form_FORM_ID_alter().
- 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.
File
- includes/
common.inc, line 6675 - 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);
}