FormBuilder unset($element['#sorted']) won't sort it later

Created on 9 July 2016, about 8 years ago
Updated 5 June 2024, 3 months ago

Please see drupal/core/lib/Drupal/Core/Form/FormBuilder.php:1038

    // If one of the child elements has a weight then we will need to sort
    // later.
    unset($element['#sorted']);

This is incorrect as the children in $element will never be sorted if it is used together with drupal/core/lib/Drupal/Core/Render/Element/Table.php:343

    foreach (Element::children($element[$first]) as $second) {
      // Assign the element by reference, so any potential changes to the
      // original element are taken over.
      $column = array('data' => &$element[$first][$second]);

      // Apply wrapper attributes of second-level elements as table cell
      // attributes.
      if (isset($element[$first][$second]['#wrapper_attributes'])) {
        $column += $element[$first][$second]['#wrapper_attributes'];
      }

      $row['data'][] = $column;
    }

See drupal/core/lib/Drupal/Core/Render/Element.php:72

    // Do not attempt to sort elements which have already been sorted.
    $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;

This will always assigning $sort to FALSE.

Suggested fix is to replace unset($element['#sorted']); with $element['#sorted'] = FALSE;

πŸ› Bug report
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡ΉπŸ‡­Thailand iamake

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand DanielVeza Brisbane, AU

    Thanks for opening an issue!

    The referenced code has been shifted around in the 7 years since this issue was opened, however the 'problem' code does still seem to exist.

    Could we get steps to replicate this on a fresh Drupal 10 install?

  • Status changed to Closed: outdated 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Since there hasn't been a follow up going to close out for now. If still an issue please reopen.

Production build 0.71.5 2024