Skip to content

Instantly share code, notes, and snippets.

@skh-
Last active January 1, 2016 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skh-/8136957 to your computer and use it in GitHub Desktop.
Save skh-/8136957 to your computer and use it in GitHub Desktop.
<?php
function formtable_form_table_form($form = array(), &$form_state) {
$form['table'] = array(
'#theme' => 'table',
'#header' => array(t('Column 1'), t('Column 2')),
'#rows' => array(
// First row.
'r1' => array(
'c1' => array(
'#type' => 'textfield',
'#title' => t('Row 1 Column 1')
),
'c2' => array(
'#type' => 'textfield',
'#title' => t('Row 1 Column 2'),
),
),
// Second row.
'r2' => array(
'c1' => array(
'#type' => 'textfield',
'#title' => t('Row 2 Column 1')
),
'c2' => array(
'#type' => 'textfield',
'#title' => t('Row 2 Column 2'),
),
),
),
);
// Add a submit button for fun.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
}
<?php
/**
* Page Callback / Form Builder for the table form.
*/
function formtable_form_table_form($form = array(), &$form_state) {
$form['table'] = array(
// Theme this part of the form as a table.
'#theme' => 'formtable_form_table',
// Pass header information to the theme function.
'#header' => array(t('Column 1'), t('Column 2')),
// Rows in the form table.
'rows' => array(
// Make it a tree for easier traversing of the entered values on submission.
'#tree' => TRUE,
// First row.
'r1' => array(
'c1' => array(
'#type' => 'textfield',
'#title' => t('Row 1 Column 1')
),
'c2' => array(
'#type' => 'textfield',
'#title' => t('Row 1 Column 2'),
),
),
// Second row.
'r2' => array(
'c1' => array(
'#type' => 'textfield',
'#title' => t('Row 2 Column 1')
),
'c2' => array(
'#type' => 'textfield',
'#title' => t('Row 2 Column 2'),
),
),
),
);
// Add a submit button for fun.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
<?php
/**
* Implementation of hook_theme().
*/
function formtable_theme() {
return array(
'formtable_form_table' => array(
// The renderable element is the form.
'render element' => 'form',
),
);
}
<?php
/**
* Theme callback for the form table.
*/
function theme_formtable_form_table(&$variables) {
// Get the useful values.
$form = $variables['form'];
$rows = $form['rows'];
$header = $form['#header'];
// Setup the structure to be rendered and returned.
$content = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => array(),
);
// Traverse each row. @see element_chidren().
foreach (element_children($rows) as $row_index) {
$row = array();
// Traverse each column in the row. @see element_children().
foreach (element_children($rows[$row_index]) as $col_index) {
// Render the column form element.
$row[] = drupal_render($rows[$row_index][$col_index]);
}
// Add the row to the table.
$content['#rows'][] = $row;
}
// Redner the table and return.
return drupal_render($content);
}
<?php
/**
* Implementation of hook_menu().
*/
function formtable_menu() {
$items = array();
// A page to demonstrate theming form elements in a table.
$items['formtable'] = array(
'title' => 'Form Table Example',
'page callback' => 'drupal_get_form',
'page arguments' => array('formtable_form_table_form'),
'access callback' => TRUE,
);
return $items;
}
/**
* Implementation of hook_theme.
*/
function formtable_theme() {
return array(
'formtable_form_table' => array(
// The renderable element is the form.
'render element' => 'form',
),
);
}
/**
* Page Callback / Form Builder for the table form.
*/
function formtable_form_table_form($form = array(), &$form_state) {
$form['table'] = array(
// Theme this part of the form as a table.
'#theme' => 'formtable_form_table',
// Pass header information to the theme function.
'#header' => array(t('Column 1'), t('Column 2')),
// Rows in the form table.
'rows' => array(
// Make it a tree for easier traversing of the entered values on submission.
'#tree' => TRUE,
// First row.
'r1' => array(
'c1' => array(
'#type' => 'textfield',
'#title' => t('Row 1 Column 1')
),
'c2' => array(
'#type' => 'textfield',
'#title' => t('Row 1 Column 2'),
),
),
// Second row.
'r2' => array(
'c1' => array(
'#type' => 'textfield',
'#title' => t('Row 2 Column 1')
),
'c2' => array(
'#type' => 'textfield',
'#title' => t('Row 2 Column 2'),
),
),
),
);
// Add a submit button for fun.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Theme callback for the form table.
*/
function theme_formtable_form_table(&$variables) {
// Get the userful values.
$form = $variables['form'];
$rows = $form['rows'];
$header = $form['#header'];
// Setup the structure to be rendered and returned.
$content = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => array(),
);
// Traverse each row. @see element_chidren().
foreach (element_children($rows) as $row_index) {
$row = array();
// Traverse each column in the row. @see element_children().
foreach (element_children($rows[$row_index]) as $col_index) {
// Render the column form element.
$row[] = drupal_render($rows[$row_index][$col_index]);
}
// Add the row to the table.
$content['#rows'][] = $row;
}
// Redner the table and return.
return drupal_render($content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment