You are here

public static function Element::children in Render cache 7.2

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

array $elements: The element array whose children are to be identified. Passed by reference.

bool $sort: Boolean to indicate whether the children should be sorted by weight.

Return value

array The array keys of the element's children.

3 calls to Element::children()
Element::getVisibleChildren in lib/Drupal/Core/Render/Element.php
Returns the visible children of an element.
RenderCacheBackendAdapter::setMultiple in src/Cache/RenderCacheBackendAdapter.php
This sets multiple cache entries based on the cache info map.
RenderStack::collectAndRemoveAssets in src/Cache/RenderStack.php

File

lib/Drupal/Core/Render/Element.php, line 75
Contains \Drupal\Core\Render\Element.

Class

Element
Provides helper methods for Drupal render elements.

Namespace

Drupal\Core\Render

Code

public static function children(array &$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 === '' || $key[0] !== '#') {
      if (is_array($value)) {
        $children[$key] = $value;
        if (isset($value['#weight'])) {
          $sortable = TRUE;
        }
      }
      elseif (isset($value)) {
        trigger_error(SafeMarkup::format('"@key" is an invalid render array key', array(
          '@key' => $key,
        )), E_USER_ERROR);
      }
    }
  }

  // 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
    // \Drupal\Core\Render\Element::children() twice.
    foreach ($children as $key => $child) {
      unset($elements[$key]);
      $elements[$key] = $child;
    }
    $elements['#sorted'] = TRUE;
  }
  return array_keys($children);
}