I'm using Drupal 7 and I need the ability for the user to choose from a list of options and put those options in order. I am using type checkbox and passing in an array of options to create the list.

I need to be able to check the options needed and sort them.

The form checkbox creation is fairly straightforward, but doesn't appear to have a weight option for the individual checkboxes or really any attributes except value.

Any help would be appreciated. Thank you.

$form[$key]['articleFields'] = array(
    '#title' => t('article Fields'),
    '#type' => 'checkboxes',
    '#description' => t('Choose the fields you want selected'),
    '#options' => $articleFields,
    '#default_value' =>$articleFields_checked,


1 Answer 1


I was able to get this working using a modified version of the code here http://dropbucket.org/node/204

The adjustments I made were to allow my table to be an element on a parent form and to repopulate the from my variables array... //TODO bubble checked boxes to top

    $rows = array();
    $row_elements = array();
    //$shared['articleFields'] values stored in variables table -- loaded in parent form 
    //$key is name of my parent form this gets attached to

    $cc=-10; //default weights starting at -10
    //field machine names with titles 
    $articleFields = array(
      'field_news_categories'=>t('News Categories'),
      'field_photo_caption_1'=>t('Featured Photo'),
      'field_text_4'=>t('Featured Quote'),
      'field_websites'=>t('Related Links'),
      'field_images'=>t('Additional Images'),
      'field_text_1'=>t('Alternate Headline'),
      'field_text_3'=>t('Alternate Subheading'),
      'field_photo_caption_2'=>t('Alternate Photo'),
      'field_longtext_1'=>t('Alternate Copy'),

    foreach ($articleFields as $k =>$v) {
        // Build the table rows.

        //if no values are defined use default weights from -10 up
        else $dv=$cc++;

        //if no value is defined use 0 for enabled value (unchecked) 
        else $dv2=0;
        $rows[$k] = array(
            'data' => array(
                // Cell for the cross drag&drop element.
                array('class' => array('entry-cross')),
                // Weight item for the tabledrag.
                array('data' => array(
                        '#type' => 'weight',
                        '#title' => t('Weight'),
                        '#title_display' => 'invisible',
                        '#default_value' => $dv,
                        '#attributes' => array(
                            'class' => array('entry-order-weight'),
                // Enabled checkbox.
                array('data' => array(
                        '#type' => 'checkbox',
                        '#title' => t('Enable'),
                        '#title_display' => 'invisible',
                        '#default_value' => t($dv2),

                // Name textfield.
                array('data' => array(
                        '#type' => 'textfield',
                        '#size' => 50,
                        '#disabled' => TRUE,
                        '#title' => t('Field'),
                        '#title_display' => 'invisible',
                        '#default_value' => t($v),


            'class' => array('draggable'),

        // Build rows of the form elements in the table.
        $row_elements[$k] = array(
            'weight' => &$rows[$k]['data'][1]['data'],
            'enabled' => &$rows[$k]['data'][2]['data'],
            'field' => &$rows[$k]['data'][3]['data'],
     //sort both the rows and the row elements on the weight value 

        uasort($rows, function($a, $b) {
            return $a['data'][1]['data']['#default_value'] <=> $b['data'][1]['data']['#default_value'];

        uasort($row_elements, function($a, $b) {
            return $a['weight']['#default_value'] <=> $b['weight']['#default_value'];

    $id=str_replace("shared_", "", $key);

    // add this to parrent form 
    $form[$key]['articleFields'] = array(
        '#prefix'=>'<div class='.$id.'>',
        '#theme' => 'table',
        // The row form elements need to be processed and build,
        // therefore pass them as element children.
        'elements' => $row_elements,
        '#header' => array(
            // We need two empty columns for the weigth field and the cross.
            array('data' => NULL, 'colspan' => 2),
        '#rows' => $rows,
        '#empty' => t('There are no entries available.'),
        '#attributes' => array('id' => 'entry-order'),
    drupal_add_tabledrag('entry-order', 'order', 'sibling', 'entry-order-weight');

enter image description here

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.