Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Last active December 21, 2015 09: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 hidayat365/6284393 to your computer and use it in GitHub Desktop.
Save hidayat365/6284393 to your computer and use it in GitHub Desktop.
Theming Form with Tables di Drupal7
<?php
/*
* form_theming_tableform
* definisi form berbentuk table
**/
function form_theming_tableform()
{
$form['people'] = array(
'#prefix' => '<div id="people">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#theme' => 'table',
'#header' => array(
t('<input type="checkbox" id="checkall" name="checkall" class="form-checkbox">'),
t('ID'), t('User'), t('Email'), t('Comment')
),
'#rows' => array(),
'#weight' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Data'),
'#weight' => 15,
);
// data sebagai contoh saja
// pada kondisi sebenarnya silakan ambil data dari database
$people = db_select('users', 'u')
->fields('u')
->condition('status', 1, '=')
->range(0,3)
->execute()
->fetchAll();
// buat array data yang akan ditampilkan sebagai table
for ($i = 0; $i < count($people); $i++) {
// Defisiniskan field-field yang akan kita jadikan row dalam table
// Untuk memudahkan pemahaman terhadap logikanya, maka kita buatkan
// variable untuk masing-masing field dan kita tambahkan secara manual.
$check = array(
'#id' => 'people-check['.$people[$i]->uid.']',
'#type' => 'checkbox',
);
$uid = array(
'#id' => 'people-name['.$people[$i]->uid.']',
'#type' => 'item',
'#markup' => $people[$i]->uid,
);
$name = array(
'#id' => 'people-name['.$people[$i]->uid.']',
'#type' => 'item',
'#markup' => $people[$i]->name,
);
$mail = array(
'#id' => 'people-mail['.$people[$i]->uid.']',
'#type' => 'textfield',
'#size' => '30',
'#default_value' => $people[$i]->mail,
);
$data = array(
'#id' => 'people-comment['.$people[$i]->uid.']',
'#type' => 'textarea',
'#size' => '30',
'#default_value' => $people[$i]->data,
);
// Masukkan field yang sudah kita buat sebelumnya sebagai data
// Perhatikan juga bahwa kita menggunakan reference
$form['people'][] = array(
'check' => &$check,
'uid' => &$uid,
'name' => &$name,
'mail' => &$mail,
'data' => &$data,
);
// Sekarang tambahkan reference yang sama ke #rows untuk digunakan
// fungsi theme_table() merender table yang kita inginkan.
// Perhatikan juga bahwa kita menggunakan reference yang sama
$form['people']['#rows'][] = array(
array('data' => &$check),
array('data' => &$uid),
array('data' => &$name),
array('data' => &$mail),
array('data' => &$data),
);
// Dan karena kita menggunakan reference, kita harus melakukan unset()
// agar nilai yang ada di variable sebelumnya tidak tertimpa pada loop berikutnya
unset($check);
unset($uid);
unset($name);
unset($mail);
unset($data);
}
return $form;
}
/*
* form_theming_tableform_validate
* fungsi validasi untuk form_theming_tableform
**/
function form_theming_tableform_validate($form, &$form_state)
{
// masukkan code validasi form di sini
// -----------------------------------
}
/*
* form_theming_tableform_validate
* fungsi submit untuk form_theming_tableform
**/
function form_theming_tableform_submit($form, &$form_state)
{
// masukkan code submit ke database di sini
// ----------------------------------------
drupal_set_message('<pre>'.print_r($form_state['values'],true).'</pre>');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment