Drupal 7 Theme Table
Recently I came across a requirement where I went through all the features in Drupal HTML tables. At times I was startled to know the efficacy of the tables in Drupal 7. I would like to share the same road trip I went through in Drupal 7. Let's begin with a step-by-step process to create tables in D7.
1. Create a path using hook_menu:
/**
* Implements hook_menu().
*/
function knackquiz_quiz_menu() {
$items = array();
$items['quiz/payments'] = array(
'title' => 'Paid Quiz list',
'page callback' => 'drupal_get_form',
'page arguments' => array('_quiz_payments_list_form'),
'access arguments' => array("access content"),
'type' => MENU_CALLBACK
);
return $items;
}
In the callback function do the following steps.,
2. The table headers have the title of each column in the table:
$header = array(
array('data' => t('Order Number'),'field' => 'order_number'),
array('data' => t('User Name'),'field' => 'name'),
array('data' => t('Quiz Title'),'field' => 'title'),
array('data' => t('Status'),'field' => 'status'),
array('data' => t('Quiz created time'),'field' => 'created'),
array('data' => t('Bought time'),'field' => 'created'),
);
Note that by default, we specify an ascending sort on the title field. This means that the rendered table will be sorted in ascending order on the title field by default. However, the user can subsequently sort on any column on the rendered table by clicking its header.
If you do not want a particular column to be sortable, do not specify the 'field' parameter for its header.
3. Next create your SQL query to be executed which returns the sorted and paged results from the database.
$query = db_select('commerce_order', 'co');
$query->leftJoin('commerce_line_item', 'li', 'li.order_id = co.order_id');
$query->leftJoin('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
$query->leftJoin('commerce_product', 'p', 'prod.commerce_product_product_id = p.product_id');
$query->leftJoin('field_data_field_product_quiz_id', 'p_nid', 'p.product_id = p_nid.entity_id');
$query->leftJoin('node', 'n', 'p_nid.field_product_quiz_id_nid = n.nid');
$query->leftJoin('users', 'u', 'co.uid = u.uid');
$result = $query
->fields('co', array('order_id', 'order_number', 'status','created'))
->fields('p', array('title'))
->fields('n',array('nid','created'))
->fields('u', array('name','uid'))
->orderBy($order, $sort)
->extend('TableSort')->extend('PagerDefault')->limit(25)
->execute();
4. Next build filter part for the form:
$form = array();
$form['filter'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Filter option')
);
$form['filter']['filter_user'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#size' => 15,
);
$form['filter']['filter_quiz'] = array(
'#type' => 'textfield',
'#title' => t('Quiz title'),
'#size' => 15,
);
$form['filter']['filter_group'] = array(
'#type' => 'textfield',
'#title' => t('Group name'),
'#size' => 15,
);
$form['filter']['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
);
5. Next Apply filter conditions
if (isset($form_state['filters']['user'])) {
$query->condition('u.name', '%' . db_like($form_state['filters']['user']) . '%', 'LIKE');
}
if (isset($form_state['filters']['quiz'])) {
$query->condition('p.title', '%' . db_like($form_state['filters']['quiz']) . '%', 'LIKE');
}
6. The next step is executing the query and collecting the rows from the resultset. This is a regular loop and requires no explanation (I hope):
$rows = array();
// Looping for filling the table rows
foreach ($result as $ord) {
// Fill the table rows
$rows[] = array(
l($ord->order_number, 'admin/commerce/orders/'. $ord->order_id .'/edit'),
l($ord->name, 'user/'. $ord->uid .'/edit'),
l($ord->title, 'node/'. $ord->nid.'/edit'),
$ord->status,
format_date($ord->created,'custom','d-M-Y',date_default_timezone()) ,
format_date($ord->n_created,'custom','d-M-Y',date_default_timezone()) ,
);
}
7 . Then, we create a table from the headers and the result rows with a simple call to $form['table']
$form['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('Table has no row!')
);
8 . The final step is to append a pager to the table.
$form['pager'] = array('#markup' => theme('pager'));
You can also make the table sortable by clicking its header. Just do the following modifications to your current code.
// Check if there is sorting request
if(isset($_GET['sort']) && isset($_GET['order'])){
// Sort it Ascending or Descending?
if($_GET['sort'] == 'asc')
$sort = 'ASC';
else
$sort = 'DESC';
// Which column will be sorted
switch($_GET['order']){
case 'Order Number':
$order = 'order_number';
break;
case 'User ID':
$order = 'name';
break;
case 'Status':
$order = 'status';
break;
case 'Product Name':
$order = 'title';
break;
case 'Quiz created time':
$order = 'created';
break;
case 'Bought time':
$order = 'created';
break;
default:
$order = 'order_id';
}
}
else {
// Default sort
$sort = 'ASC';
$order = ' order_id';
}
Then add this line after the $query->fields() command.
// Set order by
$query->orderBy($order, $sort);
This submit handler will make the filter work to get the desired result
function _quiz_payments_list_form_submit($form, &$form_state) {
$form_state['filters']['user'] = $form_state['values']['filter_user'];
$form_state['filters']['quiz'] = $form_state['values']['filter_quiz'];
$form_state['rebuild'] = TRUE;
}
In case of any suggestions/queries please do post a comment below.