Last updated October 29, 2012. Created on January 4, 2012.
Edited by leelooch, Siripong, cafuego. Log in to edit this page.

The 1st snippet displays checkboxes at the first column
like http://YOUR-DRUPAL-SITE-NAME/admin/content/node

The 2nd one, you can render checkboxes at another column,
set default values, or even render more than one checkbox on a single row
like http://YOUR-DRUPAL-SITE-NAME/admin/people/permissions

- Create a View as usual ( No page or Block require )
- If you want to add a argument, add it using Contextual Filters.

function mymodule_form($form, &$form_state) {
  $views_name = 'search_customer';
  $display_id = 'page';
    To find your display_id go to the edit page of your view.
    There will be a list of displays at the left side of the control area.
    With out creating a view or block, "Master" will be at the top of that list.
    Hover your cursor over the name of the display you want to use. and look at the URL.
    The last word will be the display ID.

  $customers = views_get_view_result($views_name , $display_id, $citizen_id);

  dpm($customers);  // it help you to navigate through $customers

  $options = array();

  // Create an options array for the checkbox from the customer nodes. Your field
  // names will differ, look them up  via the dpm() call above. You need devel.module
  // for that.
  foreach($customers as $cust) {
    $options[$cust->nid] = array(
      'first' => $cust->field_field_firstname[0]['rendered']['#markup'],
      'last' => $cust->field_field_lastname[0]['rendered']['#markup'],

  $header = array
    'first' => 'Firstname',
    'last' => 'Lastname',

  $form['customers'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,  // The array we made a bit earlier.
    '#multiple' => FALSE,

  return $form;

more info at

The 2nd way

function MYMODULE_menu(){
  $items['a_path'] = array(
    'title' => 'something',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_demo_form'),
    'access callback' => TRUE,
    'type' => MENU_LOCAL_TASK,
  return $items;

function MYMODULE_theme() {
  return array(
    'mymodule_demo_form' => array(
      'render element' => 'form',

function mymodule_demo_form($form, &$form_state){

  $view = views_get_view('a_view_name');
  $args[] = 'argument A';
  $args[] = 'argument B';
  $display_id = 'default';
  $view->execute_display($display_id, $args);
  $results = $view->result;

  foreach($results as $result) {

    $options[$result->nid] = '';

    $form['rows'][$result->nid]['column_name'] = array(
      '#markup' => $result->field_field_column_1[0]['rendered']['#markup'],

    if($result->field_field_column2[0]['raw']['value'] == '1'){
      $default_value[] = $result->nid;

  $form['checkboxes'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $default_value,

  return $form;

function theme_mymodule_demo_form($variables) {
  $form = $variables['form'];

  foreach (element_children($form['rows']) as $key) {
    $row = array();
    $row[] = array(
      'data' => drupal_render($form['rows'][$key]['column_name']),
    $row[] = array(
      'data' => drupal_render($form['checkboxes'][$key]),
      //'class' => array('checkbox'),
    $rows[] = $row;

  $header = array('column_header_1','column_header_2');

  $output = theme('table', array(
    'header' => $header,
    'rows' => $rows,

  $output .= drupal_render_children($form);
  return $output;

More info: DRUPAL-DIRECTORY/modules/user/

Looking for support? Visit the forums, or join #drupal-support in IRC.


southweb’s picture

This is great but how do you handle pagination? Is it supported seamlessly?

southweb’s picture

The trick is to roll your own pager code using the following before calling $view->execute();

pit1988’s picture

I did what you did, but not with checkboxes (only one checkbox by column and i have only 3 columns) the

 $row[] = array(
      'data' => drupal_render($form['checkbox'][$key]),
      //'class' => array('checkbox'),

Doesn't put the checkbox on the table, i don't know why, if anyone knows how to do that with checkbox