Score:-2

Adding select list to tableselect output

pt flag

I'm trying to add a select-list to some table output but the markup is rendering as text.

$header = [
  'col1' => t('COL1'),
  'col2' => t('COL2'),
];
$rows = [
  1 => [
    'col1'  =>  'row-1-cell-1',
    'col2'  =>  'row-1-cell-2',
  ],
  ['col1' => 'test'],
  ['col2' => 'test'],
  ['col1' => 'test'],
  2 => [
    'col1'  =>  'row-2-cell-1',
    'col2'  =>  $form['example_select'] = [
                  '#type' => 'select',
                  '#title' => $this->t('Select element'),
                  '#options' => [
                    '1' => $this->t('One'),
                    '2' => $this->t('Two'),
                    '3' => $this->t('Three'),
                  ],
                ]
  ],
];
$form['test'] = [
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $rows,
];

enter image description here

I have been experiementing with the code above in the buildForm(array $form, FormStateInterface $form_state) function of my controller class. The class renders a form for some custom configuration in the administration area.

Can anyone please help me understand what I need to do so my select list renders as markup instead of text ?

Score:1
fr flag

Some general advice: Don't assign numeric keys like 1 and 2 to your rows - let PHP generate the keys. Or, if you really need to set your own keys, then use string keys and give EVERY row a key. What you show above has the first row numbered 1, then the second, third and fourth row don't have keys (so PHP will assign them sequentially 2, 3, 4), then you give the fifth row the key 2 which overwrites the second row (which had an automatically-generated key of 2) - thus you end up with four rows even though you wanted five. Assigning row keys consistently from the beginning will avoid problems like this.

Likewise, when you do something like this:

'col2'  =>  $form['example_select'] = [ ... ]

you are adding a new form element to the form in TWO places - once in the table and once outside the table. If that is what you intend, then declare the form element outside the table first and just use the variable inside the table.

Anyway, the following will do what you want:

$header = [
  'col1' => $this->t('COL1'),
  'col2' => $this->t('COL2'),
];
$rows = [];

$rows[] = [
  'col1' => 'row-1-cell-1',
  'col2' => 'row-1-cell-2',
];
$rows[] = [
  'col1' => 'row-2-cell-1',
  'col2' => ['data' => [
    '#type' => 'select',
    '#title' => $this->t('Select element'),
    '#options' => [
      '1' => $this->t('One'),
      '2' => $this->t('Two'),
      '3' => $this->t('Three'),
    ],
  ]],
];

$form['test'] = [
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $rows,
];
pt flag
Wow! I was more tired than I thought. I really apologise for the messy code. I was tired and in a hurry which led to the embarrassing code mess. Thank you for your answer. It's precisely what I needed.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.