Score:0

Trying to create a cell when using tables in forms

tw flag

In one of my custom Drupal modules I'm trying to crate a form that also contains a table. One of the table sections looks like the following:

$form['consumption_' . $week]['mood']['title'] = array(
  '#markup' => $this->t('Mood'),
);

for ($i = 0; $i <= 6; $i++) {
  $options = array(
    '1' => t('choice 1'),
    '2' => t('choice 2'),
    '3' => t('choice 3'),
    '4' => t('choice 4'),
  );

  $form['consumption_' . $week]['mood']['entry_dropdown_' . $i] = array( 
    '#type' => 'select',
    '#options' => $options,
  );
                
  $form['consumption_' . $week]['mood']['entry_toggle_' . $i] = array(
    '#type' => 'radios',
    '#options' => array( 
      1 => $this->t('Yes'),
      0 => $this->t('No'),
    ),
    '#default_value' => 0,
  ); 
}

Everything works fine so far. What I would like to do now is to combine the dropdown and the radio elements so that these elements are in one cell. With the code above, they are in the same row but not in the same cell.

I've tried to work with

'#prefix' => '<td>'

and

'#suffix' => '</td>'

but I can't seem to get it to work correctly.

Any ideas on how to combine those two elements in one cell will be highly appreciated. Thanks very much!

Edit:

The code that generates the table looks like the following:

$form['consumption_' . $week] = array(
  '#type' => 'table',
  '#title' => 'XYZ',
  '#header' => array(' ', 'Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5', 'Day 6', 'Day 7'),
);
cn flag
It looks like you've forgotten to post the code which generates the table
unusedspoon avatar
aq flag
Have you seen this example https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Table.php/class/Table/ Try with `[$i]` on each item like the example
Quetzcodecoatl avatar
tw flag
Thank you so much for the help guys! The ```[$i]``` did the trick. Works like a charm! THANKS!
I sit in a Tesla and translated this thread with Ai:

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.