A Drupal theme table example

Summary: A Drupal "theme table" example, where I show how to generate an HTML table from a Drupal module, using the Drupal theme function (and specifically not the theme_table function).

I was working on a new Drupal 7 application yesterday, developing a module where I need to display a table in my Drupal module, and I couldn't find any good Drupal table examples, so I thought I'd share my working code here. This code isn't very good yet, but it does show a "Drupal theme table" example, i.e., how to display a table in Drupal by properly calling the theme function.

Without much discussion, here's my current Drupal theme table example. While I'm only on Day 1 of development, this code comes directly from a Drupal module named 'Sleetmute', and a file named sleetmute.module:

# a hook_menu implementation
function sleetmute_menu() {
  $items = array();

  # my drupal table list view menu item
  # this first line is the url ('projects')
  $items['projects'] = array(
    'title' => t('Your Projects'),
    'description' => 'Your Sleetmute FPA Projects',
    'page callback' => 'projects_list',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  return $items;

function projects_list() {
  $header = array('Cell 1', 'Cell 2');
  $data = array( 
            array('A', 'B'),
            array('C', 'D')
  $output = theme('table', 
           array('header' => $header, 
                  'rows' => $data ));
  return $output;

I'm not an expert Drupal module developer by any stretch of the imagination, but I do know that code works. It displays a simple HTML table with a header row and two data rows. Note that I call the Drupal theme function here with the 'table' attribute, and not the theme_table function. I do know that this approach is correct.

In that example I replaced my actual SQL database call with simple static table data. In the code below I'll show the actual Drupal table code, including the SQL query.

A Drupal theme table example with a database query

As a more 'real world' Drupal theme table example, here's the actual code I use to generate my HTML table, using a SQL query to get the data I need:

function get_user_id() {
  global $user;
  return $user->uid;

function projects_list() {
  $header = array('ID', 'Type', 'Name');
  $data = array();

  # set the database table
  $query = db_select('sleet_projects', 'p');

  # configure the query
  $query->condition('user_id', get_user_id())
        ->fields('p', array('id', 'project_count_type', 'name'));

  # execute the query
  $results = $query->execute();

  foreach ($results as $row) {
  $data[] = array($row->id, 
  $output = theme('table', 
                  array('header' => $header, 
  	                'rows' => $data ));
  return $output;

This example does not currently show how to add a pager to a Drupal table, but I hope to get that working later today.

Also, I should mention that this code works on Drupal 7, and will not work on Drupal 6, primarily because of the SQL functions I've used.

Drupal theme docs

As a relatively new Drupal module developer, I don't understand the Drupal theme function docs, but that's a link to them. This link on default theme implementations may also be helpful, but again, at the moment I don't understand the intent of that document. (Once I understand this process better I'll be glad to update that page, but at the moment I think that page can be seriously improved by including one or two Drupal theme function examples.)

In the meantime, if you need a Drupal theme table example, I hope this helps get you started in the right direction.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.