0

I am building a table with checkboxes without using tableselect for various reasons, but am trying to retrieve the checkbox value similar to how tableselect does. Tableselect will populate the values array in $form_state at form submission. A key with the name of the form in values contains the list of selected rows. I attempted to reverse engineer how tableselect populates the values array, but I couldn't really come up with anything other than #return_value. The values array using the following element is unpopulated when the form is submitted.

This is some of the form builder I'm using right now. It exists in the buildForm(array $form, FormStateInterface $form_state).

$rows[$nid] = [
    'check' => [
        'data' => [
            '#type' => 'checkbox',
            '#title' => 'test2',
            '#return_value' => '$nid',
        ]
    ],
],

// …

$form['table'] = array(
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
);
2
  • Welcome to Drupal Answers! As side note, '#return_value' => '$nid' isn't setting #return_value with the value of $nid: It's setting it to a string containing the $, n, i, and d characters.
    – apaderno
    May 16, 2021 at 7:48
  • As previous commenter said, if you are trying to return the value of the nid by it's variable, make it '#return_value' => $nid, as '$nid' will literally print '$nid' as the return value in a string.
    – Ex0r
    May 16, 2021 at 14:37

1 Answer 1

0

You probably want to use '#type' => 'tableselect' as the form field type instead of 'table'. tableselect is a table type definition that provides a select box to the table.

From there, you can build your options for the table like so:

$options[
  <return_value> => [
   'field_name' => 'value',
   'field_name2' => 'value', 
  ],
];

Or in your case:

$options[
  <nodeid> => [
   'field1' => 'value', 
   'field2' => 'value',
  ],
];

More information on the tableselect type can be found here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Tableselect.php/class/Tableselect/8.2.x

I know you mentioned not wanting to use tableselect, but save yourself the headache and don't reinvent the wheel. That's what tableselect is for :)

The table form aAPI element is a display element used for displaying tabular data, not being used as a valid form input method.

4
  • I guess the OP asked this question because the issues described in drupal.stackexchange.com/questions/303018/….
    – apaderno
    May 16, 2021 at 15:30
  • Possible, but like 'table', 'link' is a display element not an input element.
    – Ex0r
    May 16, 2021 at 17:11
  • tableselect is already a table with checkboxes. Indeed, the OP should avoid writing his own implementation of tableselect. I just meant that the other question should be taken in consideration, while pointing the user to avoid implementing what Drupal already offers.
    – apaderno
    May 16, 2021 at 17:50
  • tableselect puts a checkbox at the start of each row and nothing else. If the person wants something else, like a checkbox in a different place or in a cell with a rowspan attribute, it can't be done. Dec 9, 2022 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.