1 common.inc element_children(&$elements, $sort = FALSE)

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 backdrop_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.:

File

core/includes/common.inc, line 7439
Common functions that many Backdrop 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.
  $children = array();
  $sortable = FALSE;
  foreach ($elements as $key => $value) {
    if ($key === '' || substr($key, 0, 1) !== '#') {
      if (is_array($value)) {
        $children[$key] = $value;
        if (isset($value['#weight'])) {
          $sortable = TRUE;
        }
      }
      // Only trigger an error if the value is not null.
      // @see http://drupal.org/node/1283892
      elseif (isset($value)) {
        trigger_error(t('"@key" is an invalid render array key', array('@key' => $key)), E_USER_ERROR);
      }
    }
  }
  // Sort the children if necessary.
  if ($sort && $sortable) {
    backdrop_sort($children, array('#weight'));
    // 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;
  }

  return array_keys($children);
}