tableselect is incompatible with #tabledrag

Created on 15 November 2024, about 1 month ago

Problem/Motivation

Tableselect FAPI element is super helpful for (multi)selecting values in tables.

Sadly I had to find out, that the widely used #tabledrag sorting functionality on tables (e.g. sorting taxonomy terms, labels, contrib...) doesn't seem to be compatible with $element['#type'] = 'tableselect';.

The key issue is, that #options doesn't seem to be capable of render arrays? (Or I was unable to find out how!).

#tabledrag requires to place a '#type' => 'weight', element into each row, but doing that like this:

$options[$key] = [
        'label' => [
          '#markup' => $option,
        ],
        // @todo This syntax doesn't work with tableselect,
        // also tried with 'data' and placing the values
        // directly into the $element without success!
        'weight' => [
          '#type' => 'weight',
          '#delta' => 50,
          '#title' => $this->t('Weight for @title', ['@title' => (string) $option]),
          '#title_display' => 'invisible',
          '#default_value' => $weight,
          '#attributes' => ['class' => [$tabledragGroup]],
        ],
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
      ];

doesn't work. Instead tableselect renders each value in 'weight' as separate column.

I also tried the alternative syntax from Table documentation:

#rows: An array of the rows to be displayed. Each row is either an array of cell contents or an array of properties as described in table.html.twig Alternatively specify the data for the table as child elements of the table element. Table elements would contain rows elements that would in turn contain column elements.

but then no options are rendered.

Looks like an incompatibility in class Tableselect?

Still it would be super helpful for developers to have this working out of the box. There are several cases where this would help and reduce the need for custom coding!

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

forms system

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Comments & Activities

Production build 0.71.5 2024