multiple columns within form fields using form api theming
Rating:
Clone this SnippetSubmitted 7 years 8 months ago by peter.
- Log in to post comments
Change the form on a node to support multiple rows
/** * Implements hook_theme(). */ function custom_theme() { return array( 'multicolumn_options' => array( 'render element' => 'form', ), ); } function theme_multicolumn_options($element) { // Modify to dig one layer deeper. Not sure why original code didn't need this. $stripped_element = array_values($element); $element = $stripped_element[0]; // Initialize variables. $output = ''; $total_columns = $element['#multicolumn_numcols']; $total_options = count($element['#options']); $options_per_column = ceil($total_options / $total_columns); $keys = array_keys($element['#options']); $type = $element[$keys[0]]['#type']; $output .= '<div class="multicolumn-options-wrapper">'; $current_column = 1; $current_option = 0; while ($current_column <= $total_columns) { $output .= '<div class="multicolumn-options-column" style="width: ' . 100 / $total_columns . '%; float: left">'; // or you run out of options. while ($current_option < $options_per_column * $current_column && $current_option < $total_options) { // Output as either check or radio button depending on the element type. $output .= drupal_render($element[$keys[$current_option]]); $current_option++; } // End column div. $output .= '</div>'; $current_column++; } $output .= '</div>'; $output .= '<div class="clear-both"></div>'; return $output; } function custom_form_alter(&$form, &$form_state, $form_id) { if($form_id == 'THEFORMID') { $form['field_drupal_category']['und']['#theme'] = array('multicolumn_options'); $form['field_drupal_category']['und']['#multicolumn_numcols'] = 4; } }
Category:
Drupal