You are here

function token_element_children in Token 8

2 calls to token_element_children()
token_render_array in ./token.module
Do not use this function yet. Its API has not been finalized.
token_tokens in ./token.tokens.inc
Implements hook_tokens().

File

./token.module, line 581
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_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 = [];
  $sortable = FALSE;
  foreach ($elements as $key => $value) {
    if (is_int($key) || $key === '' || $key[0] !== '#') {
      $children[$key] = $value;
      if (is_array($value) && isset($value['#weight'])) {
        $sortable = TRUE;
      }
    }
  }

  // Sort the children if necessary.
  if ($sort && $sortable) {
    uasort($children, 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');

    // 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);
}