How to Set Defaults in Drupal's Tableselect Form Element

This may be entirely obvious to those of you who work with forms all the time and create tableselect form elements in Drupal, but for some reason I had a hard time with it. So if you ever get tripped up setting default selections in Drupal's tableselect form element this might help. (Or at least it will help me the next time I need it!)

The code I was working on was a simple tableselect field that shows a product, size, price, and the remaining inventory of each product's size. This last bit was key because the client for whom I was developing this module absolutley required that remaining inventory be shown to prospective buyers. He had only so many to sell; and his promise to buyers was the limited number of originals - a pretty compelling proposition.

The need to set defaults in a Drupal tableselect field element was essential here because a prospective buyer would see a multi-part form for checkout. Navigating back to the product page via the "Previous" button made it necessary to show the buyer which products he had selected.

Here's the code for the original #options portion of my Drupal tableselect form element:

<?php       
$options
= array();
   
$product = $form_state['product'];
   
myModule_get_inventory($product);
    foreach (
$product->fields as $field => $data) {
       
$options[$field] = array(
           
'name' => array('data' => $field, 'class' => 'field',),
           
'size' => array('data' => $data['size'], 'class' => 'size',),
           
'inventory' => array('data' => $data['inventory'], 'class' => 'inventory',),
           
'price' => array('data' => $data['price'], 'class' => 'price',)
        );
    }
?>

Basically all we're doing here is iterating over the list of products to be included in this tableselect element and plugging the various product fields into their respective columns. Drupal's tableselect form element creates the checkboxes or radio buttons automatically for us.

The part that tripped me up was setting defaults for this element when the user navigated backwards in the multi-part form. If he had made one or more selections it was important to re-select those options for him. Here's the code that did the trick:

<?php       
// if we got to this form by selecting the "previous" button on the confirmation form
// we want to pre-select the default values for the tableselect element
   
if (isset($form_state['page_values'][1])) {
       
$productsChosen = $form_state['page_values'][1]['product'];
       
$defaults = array();
        foreach (
$productsChosen as $key => $value) {
            if (
$key === $value) {
               
$defaults[$key] = 1;
            }
        }
    }
?>

Simple, yes. It seems so obvious looking at it now; but for some reason I struggled with it. Next time I won't. And hopefully if you're here because you have the same mental block this will get you through it in no time at all.

Powered by Drupal, an open source content management system