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

Comments

anybody created an issue. See original summary.

anybody’s picture

Title: tableselect is incompatible with #tabledrag » FormElement tableselect is incompatible with #tabledrag
anybody’s picture

Issue summary: View changes

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.