Meta split from #2448765: Element::children sort order undefined and slower than it could be - This makes tests fail in PHP7.
Problem/Motivation
- Core uses uasort in quite a few places, but uasort does not preserve the sort order
Hence while:
<?php
$test = [
'foo:1' => [ '#markup' => 'foo - first' ],
'bar:2' => [ '#markup' => 'bar - second' ],
];
?>
works
<?php
$test = [
'foo:1' => [ '#markup' => 'foo - first' ],
'bar:2' => [ '#markup' => 'bar - second' ],
'first:0' => [ '#markup' => 'little things', '#weight' => -1000 ],
];
?>
does not work when run via Element::children($test, TRUE); (until #2448765: Element::children sort order undefined and slower than it could be - This makes tests fail in PHP7 is committed)
It returns:
Array
(
[0] => first:0
[1] => bar:2
[2] => foo:1
)
which is wrong.
This affects Drupal 7 as well as 8 and is the reason why explicit weights are needed so often in 7 even though it _should_ work without.
Proposed resolution
- Add a generic drupalUASort functionality that does preserve the weights.
- Convert all users of uasort to this helper class to ensure we don't have inconsistency in core with very weird weights bugs later.
Remaining tasks
- Discuss
- Fix generically