public static Element::children(array &$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 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.
File
- core/lib/Drupal/Core/Render/Element.php, line 71
Class
- Element
- Provides helper methods for Drupal render elements.
Namespace
Drupal\Core\Render
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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 = array (); $i = 0; $sortable = FALSE; foreach ( $elements as $key => $value ) { if ( $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 ; } // Only trigger an error if the value is not null. elseif (isset( $value )) { trigger_error(SafeMarkup::format( '"@key" is an invalid render array key' , array ( '@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 ); } |
Please login to continue.