Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.

Adding checkboxes/Radios to table

Last updated on
December 1, 2016 - 17:53

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

First
===
- 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 http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
and http://drupal.org/node/945102

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/user.admin.inc