Skip to content

Instantly share code, notes, and snippets.

@neotohin
Created May 30, 2013 11:26
Show Gist options
  • Save neotohin/5677232 to your computer and use it in GitHub Desktop.
Save neotohin/5677232 to your computer and use it in GitHub Desktop.
Drupal : Make fieldset Items Tabular
/**
* Implements hook_theme().
*/
function example_theme($existing, $type, $theme, $path) {
return array(
'table_layout' => array(
'render element' => 'form',
'file' => 'includes/st_chart.theme.inc'
),
);
}
/**
* Render form items as table
* Takes additionl options via #option tag
*
* '#options' => array(
* 'column' => 2, // Number for Columns
* 'header' => array( array('data' => 'Header1') , array( 'data' => 'Header 2') ),
* 'attributes' => array('id' => 'settings_form', 'style' => 'width: 55% !important;')
* ),
*
*/
function theme_table_layout( $variables)
{
$form = $variables['form'];
$settings = $form['#options'];
unset($form['#options']);
$rows = array();
$key = 0;
$chunks = array_chunk( element_children($form), $settings['column']);
foreach($chunks as $row ) {
foreach( $row as $el_name )
$rows[$key]['data'][] = array( 'data' => drupal_render( $form[ $el_name ] ) );
$key++;
}
$header = $settings['header'];
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => $settings['attributes'],
));
$output .= drupal_render_children($form);
return $output;
}
// Now Usage in Code
// Using butter class :)
function my_sample_form( $form, &$form_state) {
$_form = new butter( $form );
$f = "fieldset_1";
$_form->fieldset( $f, 'Chart Settings',
array(
'#options' => array(
'column' => 2,
'header' => array(
array( 'data' => 'Name'),
array( 'data' => 'Value'),
),
'attributes' => array('id' => 'samle-table')
),
'#theme' => 'table_layout'
));
$_form->textField( "name_1", $f, '', "Name");
$_form->textField( "value_1", $f, '', "value" );
$_form->textField( "name_2", $f, '', "Name");
$_form->textField( "value_2", $f, '', "value" );
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment