'fieldset', '#title' => t('Multicolumn checkbox/radio buttons widget display settings'), '#description' => t('Adjust settings to display checkbox/radio buttons widget in columns.'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => 2, ); $v['multicolumncheckboxesradios']['mccr_width'] = array( '#type' => 'select', '#title' => t('Columns'), '#options' => array(0 => t('Multi-column off'), 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9'), '#default_value' => isset($widget['mccr_width']) ? $widget['mccr_width'] : 1, '#description' => t('The number of columns desired.'), ); $v['multicolumncheckboxesradios']['mccr_row-major'] = array( '#type' => 'checkbox', '#title' => t('Row-major'), '#default_value' => isset($widget['mccr_row-major']) ? $widget['mccr_row-major'] : 0, '#description' => t('Select this to display check boxes/radios button across the screen first, then down.'), ); $v['multicolumncheckboxesradios']['mccr_indent'] = array( '#type' => 'select', '#title' => t('Indent'), '#options' => array(0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8'), '#default_value' => isset($widget['mccr_indent']) ? $widget['mccr_indent'] : 0, '#description' => t('The number of indentation desired.'), ); $v['multicolumncheckboxesradios']['mccr_caption'] = array( '#type' => 'textfield', '#title' => t('Caption'), '#default_value' => isset($widget['mccr_caption']) ? $widget['mccr_caption'] : '', '#description' => t('Table caption for the multicolumn checkboxes/radios.'), ); $v['multicolumncheckboxesradios']['mccr_column-heading'] = array( '#type' => 'textarea', '#title' => t('Column heading'), '#default_value' => isset($widget['mccr_column-heading']) ? $widget['mccr_column-heading'] : '', '#description' => t('The column heading each on a separate line.'), ); $v['multicolumncheckboxesradios']['mccr_row-heading'] = array( '#type' => 'textarea', '#title' => t('Row heading'), '#default_value' => isset($widget['mccr_row-heading']) ? $widget['mccr_row-heading'] : '', '#description' => t('The row heading each on a separate line.'), ); break; case 'save': $v = array_merge($v, array('mccr_width', 'mccr_row-major', 'mccr_indent', 'mccr_caption', 'mccr_column-heading', 'mccr_row-heading')); break; } } } function multicolumncheckboxesradios_elements() { return array( 'checkboxes' => array( '#multicolumn' => FALSE, '#process' => array('multicolumncheckboxesradios_element_process'), ), 'radios' => array( '#multicolumn' => FALSE, '#process' => array('multicolumncheckboxesradios_element_process'), ), ); } /** * Form element process function to add multi-column markup to checkboxes/radios * * @param $element * @return the processed element */ function multicolumncheckboxesradios_element_process($element, $edit, &$form_state, $form) { // only do this if it's coming from CCK, otherwise, let the normal form element through if (empty($element['#multicolumn']) && isset($form['#field_info'])) { $widget = &$form['#field_info'][$element['#parents'][0]]['widget']; $temp = $widget['mccr_column-heading']; if (!empty($temp)) { $column_heading = explode("\r", trim($temp)); } $temp = $widget['mccr_row-heading']; if (!empty($temp)) { $row_heading = explode("\r", trim($temp)); } $element['#multicolumn'] = array( 'width' => $widget['mccr_width'], 'row-major' => $widget['mccr_row-major'], 'indent' => $widget['mccr_indent'], 'caption' => $widget['mccr_caption'], 'column-heading' => $column_heading, 'row-heading' => $row_heading, ); } // if multicolumn option is not set or width parameter is not set, do nothing if (empty($element['#multicolumn']) || empty($element['#multicolumn']['width'])) { return $element; } $item_total = count($element['#options']) + (!empty($element['#multicolumn']['indent']) ? $element['#multicolumn']['indent'] : 0); // in case there are fewer items than the width, then the width is just the // item_total in one row $width = min($item_total, $element['#multicolumn']['width']); // if we end up with no elements, do nothing if ($width < 1) { return $element; } // copy and unset the checkboxe/radios child elements, we will rearrange them $keys = array_keys($element['#options']); $element_total = count($keys); $copy = array(); for ($i = 0 ; $i < $element_total ; ++$i) { $copy[$keys[$i]] = $element[$keys[$i]]; unset($element[$keys[$i]]); } $space = array( '#type' => 'markup', '#value' => ' ', ); if (!empty($element['#multicolumn']['indent'])) { $indent = $element['#multicolumn']['indent']; $spaces = array(); for ($i = 0 ; $i < $indent ; ++$i) { $spaces["multicolumncheckboxesradios-space-$i"] = $space; } $copy = $spaces + $copy; } if (empty($element['#multicolumn']['row-major'])) { $rows = _multicolumncheckboxesradios_chunk_transpose($copy, $width); } else { $rows = array_chunk($copy, $width, TRUE); } // fill the last row tail end with space if it's shorter than $width $last_row = &$rows[count($rows) - 1]; while (($last_row_size = count($last_row)) < $width) { $last_row["multicolumncheckboxesradios-tail-space-$last_row_size"] = $space; } // prepend the row heading in each row $has_row_heading = !empty($element['#multicolumn']['row-heading']); if ($has_row_heading) { $i = 0; $row_count = count($rows); foreach ($element['#multicolumn']['row-heading'] as $h) { if ($i < $row_count) { $rows[$i] = array("multicolumncheckboxesradios-row-heading-$i" => array('#type' => 'markup', '#value' => $h)) + $rows[$i]; } else { $rows[$i] = array("multicolumncheckboxesradios-row-heading-$i" => array('#type' => 'markup', '#value' => $h)); } ++$i; } for ( ; $i < $row_count ; ++$i) { $rows[$i] = array("multicolumncheckboxesradios-row-heading-$i" => $space) + $rows[$i]; } } $markup = ''; $table_class = 'class="multicolumncheckboxesradios-table"'; $has_column_heading = !empty($element['#multicolumn']['column-heading']); if ($has_column_heading) { $table_class = 'class="multicolumncheckboxesradios-table sticky-enabled"'; drupal_add_js('misc/tableheader.js'); $markup = _multicolumncheckboxesradios_make_heading_markup($element['#multicolumn']['column-heading'], 'multicolumncheckboxesradios-column-heading', 'multicolumncheckboxesradios-column-heading-first', 'multicolumncheckboxesradios-column-heading-last'); if ($has_row_heading) { $markup = '
| "; $row[$keys[0]]['#suffix'] = ' | |||
| "; $row[$keys[0]]['#suffix'] = ' | |||
| "; $row[$keys[0]]['#suffix'] = ' | '; $row[$keys[1]]['#prefix'] = ""; $row[$keys[1]]['#suffix'] = ' | ||
| "; $row[$keys[0]]['#suffix'] = ' | '; $row[$keys[1]]['#prefix'] = ''; $row[$keys[1]]['#suffix'] = ' | ||
| "; $row[$keys[0]]['#suffix'] = ' | '; $row[$keys[1]]['#prefix'] = ''; $row[$keys[1]]['#suffix'] = ' | '; $end_index = $row_size - 1; for ($i = 2 ; $i < $end_index ; ++$i) { $row[$keys[$i]]['#prefix'] = ''; $row[$keys[$i]]['#suffix'] = ' | '; } $row[$keys[$end_index]]['#prefix'] = ''; $row[$keys[$end_index]]['#suffix'] = ' |
| "; $row[$keys[0]]['#suffix'] = ' | '; $end_index = $row_size - 1; for ($i = 1 ; $i < $end_index ; ++$i) { $row[$keys[$i]]['#prefix'] = ''; $row[$keys[$i]]['#suffix'] = ' | '; } $row[$keys[$end_index]]['#prefix'] = ''; $row[$keys[$end_index]]['#suffix'] = ' |