You are here

public static function Element::children in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element::children()
  2. 9 core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element::children()

Identifies the children of an element array, optionally sorted by weight.

The children of an element array are those key/value pairs whose key does not start with a '#'. See \Drupal\Core\Render\RendererInterface::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.

102 calls to Element::children()
AccountSettingsForm::buildForm in core/modules/user/src/AccountSettingsForm.php
Form constructor.
Actions::preRenderActionsDropbutton in core/lib/Drupal/Core/Render/Element/Actions.php
#pre_render callback for #type 'actions'.
AddFormBase::preRenderAddedMedia in core/modules/media_library/src/Form/AddFormBase.php
Converts the set of newly added media into an item list for rendering.
BookAdminEditForm::submitForm in core/modules/book/src/Form/BookAdminEditForm.php
Form submission handler.
ckeditor5_form_filter_format_form_alter in core/modules/ckeditor5/ckeditor5.module
Implements hook_form_FORM_ID_alter().

... See full list

File

core/lib/Drupal/Core/Render/Element.php, line 72

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.
  $count = count($elements);
  $child_weights = [];
  $i = 0;
  $sortable = FALSE;
  foreach ($elements as $key => $value) {
    if (is_int($key) || $key === '' || $key[0] !== '#') {
      if (is_array($value)) {
        if (isset($value['#weight'])) {
          $weight = $value['#weight'];
          $sortable = TRUE;
        }
        else {
          $weight = 0;
        }

        // Supports weight with up to three digit precision and conserve
        // the insertion order.
        $child_weights[$key] = floor($weight * 1000) + $i / $count;
      }
      elseif (isset($value)) {
        trigger_error(new FormattableMarkup('"@key" is an invalid render array key', [
          '@key' => $key,
        ]), E_USER_ERROR);
      }
    }
    $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
    // \Drupal\Core\Render\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);
}