Skip to main content
added 200 characters in body
Source Link
Whiskey
  • 1.4k
  • 2
  • 20
  • 39

I adapted Berdir's solution for display in columns and Drupal 7. Thought I'd share it.

function yourmodule_theme() {
  return array
      (
      'form_yourmodule_form' => array
          (
          'render element' => 'form'
      ),
  );
}

function theme_form_yourmodule_form($variables) {

  $form = $variables['form'];

  $element = $form['checkboxelement'];
  unset($form['checkboxelement']);

  $elements = element_children(form_process_checkboxes($element));

  $colnr = 3;   // set nr of columns here

  $itemCount = count($elements);
  $rowCount = $itemCount / $colnr;
  if (!is_int($rowCount))
    $rowCount = round((($itemCount / $colnr) + 0.5), 0, PHP_ROUND_HALF_UP);
  $rows = array();
  for ($i = 0; $i < $rowCount; $i++) {
    $row = array();
    for ($col = 0; $col < $colnr; $col++) {
      if (isset($elements[$i + $rowCount * $col]))
        $row[] = drupal_render($element[$elements[$i + $rowCount * $col]]);
    }
    $rows[] = $row;
  }

  $variable = array(
      'header' => array(),
      'rows' => $rows,
      'attributes' => array('class' => 'checkbox_columns'),
      'caption' => NULL,
      'colgroups' => NULL,
      'sticky' => NULL,
      'empty' => NULL,
  );


  $output = theme_table($variable);

  $output .= drupal_render_children($form);

  return $output;
}

I found an even easier solution: there is a module for this! Multi-column checkboxes radios

I adapted Berdir's solution for display in columns and Drupal 7. Thought I'd share it.

function yourmodule_theme() {
  return array
      (
      'form_yourmodule_form' => array
          (
          'render element' => 'form'
      ),
  );
}

function theme_form_yourmodule_form($variables) {

  $form = $variables['form'];

  $element = $form['checkboxelement'];
  unset($form['checkboxelement']);

  $elements = element_children(form_process_checkboxes($element));

  $colnr = 3;   // set nr of columns here

  $itemCount = count($elements);
  $rowCount = $itemCount / $colnr;
  if (!is_int($rowCount))
    $rowCount = round((($itemCount / $colnr) + 0.5), 0, PHP_ROUND_HALF_UP);
  $rows = array();
  for ($i = 0; $i < $rowCount; $i++) {
    $row = array();
    for ($col = 0; $col < $colnr; $col++) {
      if (isset($elements[$i + $rowCount * $col]))
        $row[] = drupal_render($element[$elements[$i + $rowCount * $col]]);
    }
    $rows[] = $row;
  }

  $variable = array(
      'header' => array(),
      'rows' => $rows,
      'attributes' => array('class' => 'checkbox_columns'),
      'caption' => NULL,
      'colgroups' => NULL,
      'sticky' => NULL,
      'empty' => NULL,
  );


  $output = theme_table($variable);

  $output .= drupal_render_children($form);

  return $output;
}

I adapted Berdir's solution for display in columns and Drupal 7. Thought I'd share it.

function yourmodule_theme() {
  return array
      (
      'form_yourmodule_form' => array
          (
          'render element' => 'form'
      ),
  );
}

function theme_form_yourmodule_form($variables) {

  $form = $variables['form'];

  $element = $form['checkboxelement'];
  unset($form['checkboxelement']);

  $elements = element_children(form_process_checkboxes($element));

  $colnr = 3;   // set nr of columns here

  $itemCount = count($elements);
  $rowCount = $itemCount / $colnr;
  if (!is_int($rowCount))
    $rowCount = round((($itemCount / $colnr) + 0.5), 0, PHP_ROUND_HALF_UP);
  $rows = array();
  for ($i = 0; $i < $rowCount; $i++) {
    $row = array();
    for ($col = 0; $col < $colnr; $col++) {
      if (isset($elements[$i + $rowCount * $col]))
        $row[] = drupal_render($element[$elements[$i + $rowCount * $col]]);
    }
    $rows[] = $row;
  }

  $variable = array(
      'header' => array(),
      'rows' => $rows,
      'attributes' => array('class' => 'checkbox_columns'),
      'caption' => NULL,
      'colgroups' => NULL,
      'sticky' => NULL,
      'empty' => NULL,
  );


  $output = theme_table($variable);

  $output .= drupal_render_children($form);

  return $output;
}

I found an even easier solution: there is a module for this! Multi-column checkboxes radios

Source Link
Whiskey
  • 1.4k
  • 2
  • 20
  • 39

I adapted Berdir's solution for display in columns and Drupal 7. Thought I'd share it.

function yourmodule_theme() {
  return array
      (
      'form_yourmodule_form' => array
          (
          'render element' => 'form'
      ),
  );
}

function theme_form_yourmodule_form($variables) {

  $form = $variables['form'];

  $element = $form['checkboxelement'];
  unset($form['checkboxelement']);

  $elements = element_children(form_process_checkboxes($element));

  $colnr = 3;   // set nr of columns here

  $itemCount = count($elements);
  $rowCount = $itemCount / $colnr;
  if (!is_int($rowCount))
    $rowCount = round((($itemCount / $colnr) + 0.5), 0, PHP_ROUND_HALF_UP);
  $rows = array();
  for ($i = 0; $i < $rowCount; $i++) {
    $row = array();
    for ($col = 0; $col < $colnr; $col++) {
      if (isset($elements[$i + $rowCount * $col]))
        $row[] = drupal_render($element[$elements[$i + $rowCount * $col]]);
    }
    $rows[] = $row;
  }

  $variable = array(
      'header' => array(),
      'rows' => $rows,
      'attributes' => array('class' => 'checkbox_columns'),
      'caption' => NULL,
      'colgroups' => NULL,
      'sticky' => NULL,
      'empty' => NULL,
  );


  $output = theme_table($variable);

  $output .= drupal_render_children($form);

  return $output;
}