In Drupal 7, I could achieve this using the following code.

$form['table'] = array(
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $options,
  '#empty' => t('No data')

This doesn't work anymore in Drupal 8. I just want a page similar to the one at /admin/people. What's the correct way in Drupal 8?

  • You can look at his they rendered the /admin/people page
    – Eyal
    Commented Jan 10, 2016 at 18:46
  • I think admin/people is a view, in Drupal 8. The OP is asking what is the code that they should use in Drupal 8, and that is equivalent for the Drupal 7 code shown in the question.
    – apaderno
    Commented Jan 10, 2016 at 18:51

3 Answers 3


The form element type "tableselect" is available in Drupal 8

Description: A table created with a far left column of radios or checkboxes. Build the table headings and columns with the #headers property, and the rows with the #options property.


More info on "Adding checkboxes to a table" can be found in the handbook documentation here: https://www.drupal.org/node/945102

  • 1
    it doesn't work! i tried this:'#type' => 'tableselect', '#header' => $header, '#options' => $rows, '#multiple' => TRUE, '#empty' => $this->t('No data .'), ); but the result is empty
    – JIA
    Commented Jan 11, 2016 at 2:06
  • @benson, I suspect it will be an error in your code and not in the core API. Please check the above handbook link and verify your implementation is correct. Commented Jan 11, 2016 at 8:14
  • 1
    i did read the documentary carefully and tried it many times. Please note it worked perfectly in the Drupal 7. i spent many hours trying to find an example in the Drupal 8 source code and failed to find any module using the tableselect render.
    – JIA
    Commented Jan 12, 2016 at 9:20

I spent many hours to figure this out and hope my code will help someone to save time. I am using Drupal 8 version I was working on some same sort of code where I wanted to output a listing of nodes after form submit and the listing should have checkbox in each row. Below is the sample code. Please modify as per your requirements.

$form['mytable'] = array(
    '#type' => 'table',
    '#title' => 'List of Nodes',
    '#header' => ["Checkbox","Title","Author"],

$nids = \Drupal::entityQuery('node')
        ->condition('status', 1)
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$nids = array_keys($nodes);

// Add input fields in table cells.
foreach($nids as $nid) 

  $node = \Drupal\node\Entity\Node::load($nid);
  $receivername = $node->getOwner()->getDisplayName();

    $form['mytable'][$i][''] = array(
                                        '#type' => 'checkbox',
                                        '#return_value' => $nid,

    $form['mytable'][$i]['Title'] = array(
                                        '#type' => 'label',
                                        '#title' => t($contenttitle),
    $form['mytable'][$i]['Author'] = array(
                                        '#type' => 'label',
                                        '#title' => t($receivername),
// Add a submit button that handles the submission of the form.
$form['actions']['submit'] = [
                                '#type' => 'submit',
                                '#value' => $this->t('Submit'),
return $form;

below is the output example enter image description here

  • It looks like you are loading the node twice. Once with all of the nodes, and again inside the loop
    – jgreep
    Commented Mar 31 at 0:53

You may be missing that the keys of header and respective keys of option should be same. Either both indexed or both associative. I face same issue having index key in header and associative key in option which gives empty table. https://www.drupal.org/node/945102 code will work

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.