0

I'm trying to add a checkbox column in the admin table part of my module. The idea is to add checkboxes at the beginning of each row, like in the normal admin content management form.

My table is build in a form function with the following code :

function myform_admin_form($form, &$form_state) { 

$query = db_select('form_xml', 'f');
$query->join('form_xml_status', 's', 's.id = f.id');
$query->join('users', 'u', 'u.uid = f.uid');
$result = $query
        ->condition('s.status', 'todo', '=')
        ->fields('f')
        ->fields('u', array('name'))
        ->execute()
        ->fetchAll();

$header = array(
array('data' => t('ID')),  
array('data' => t('Name')),  
array('data' => t('User')), 
);

foreach($result as $key){
$rows[] = array(
array('data' => t($key->id)), 
array('data' => t($key->name)), 
array('data' => t($key->user))
);
} 

$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
);

$output = theme('table', array('header' => $header, 'rows' => $rows));
$output .= $form;

return $output;

}

My tableselect doesn't display at all, but "Array" appears at the bottom of the table. Is it possible to add checkboxes in a table like this ?

  • and the question is...? – jonhattan Jul 3 '13 at 9:07
0

Although there's actually no question, I've noted the last part of your code should look like:

<?php

$form['table'] = array(
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $rows,
);

return $form;
  • Oops, I forget the question... The problem is that I'm returning the theme table and I try to also return a form. – user18713 Jul 3 '13 at 9:22
  • I guess you mean neither of both ways works for you. Please update the OP and add the code you are using to load the form, ie. hook_menu(), drupal_get_form()... – jonhattan Jul 4 '13 at 10:24
0

I have worked on some similar script 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.

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


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

// Add input fields in table cells.
$i=0;
foreach($nids as $nid) 
{

  $node = \Drupal\node\Entity\Node::load($nid);
  $contenttitle=$node->title->value;
  $receivername = $node->getOwner()->getDisplayName();
  \Drupal::entityManager()->getStorage('node')->resetCache(array($nid));

    $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),
                                    );
    $i++;
}
// Add a submit button that handles the submission of the form.
$form['actions']['submit'] = [
                                '#type' => 'submit',
                                '#value' => $this->t('Submit'),
                            ];
return $form;

Hope that Helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy