Create a Drupal table list view from a Drupal module

Drupal table view FAQ: How do I create a simple Drupal table view in Drupal module? I want to create a simple "list view", the sort of list view you might see in a simple CRUD application.

I just ran into this Drupal "table list" problem while creating a Drupal module, and after a lot of digging around, I found a formula that lets me display a Drupal table view like this:

A simple Drupal table list view example

I haven't added a pager to this table view yet, but if you're interested in how to display a Drupal table view from a Drupal module, read on.

Creating a simple table view from a Drupal module

The secret to displaying a Drupal table view from a Drupal module is to call the Drupal theme function to generate your output. I demonstrate how this is done in the following Drupal module function:

function minime_reminders_list() {
  $header = array('ID', 'Event', 'Event Time');
  $data = array();
  $query = db_query("SELECT id, event, event_time FROM {minime_reminders}");
  while ($row = db_fetch_array($query)) $data[] = $row;
  $output = theme('table', $header, $data);
  return $output;
}

As you can see, I create a $header variable with three elements in it (ID, Event, and Event Time), then get my data from the database to populate the $data array. As you can see from the SQL call, my SQL SELECT statement returns three fields which correspond to my Drupal table header columns. I made the Drupal theme function call bold so you can see it more easily.

(Note: In an earlier version of this article I called the Drupal theme_table function instead of calling the theme function. Calling theme_table directly is actually a bad practice, as it doesn't allow a theme to override this. So, use theme() instead of theme_table().)

Creating a Drupal module menu item

I display my Drupal table view when the user clicks a menu item I've named "Your Reminders". I set up this Drupal menu item using the code shown here:

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

  # my drupal table list view menu item
  $items['minime/reminders'] = array(
    'title' => t('Your Reminders'),
    'page callback' => 'minime_reminders_list',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,  # this creates a menu item
  );

  # more code here ...

  return $items;
}

As you can see in this Drupal menu function (an implementation of hook_menu), when the user clicks this menu item, I display my Drupal table list view using the function shown above. The "page callback" setting in this function is the link between the menu item and the Drupal table view function.

Drupal table list view - Summary

I hope this Drupal table list view tutorial is helpful. I'll add more to this example -- including adding a table pager -- as I learn more about this Drupal table view approach. Until then, I hope this saves you some time.

Drupal Table View <> View with Table

This looks nice. You can also use the views module to display tables.

Theme call is wrong.

theme_table($header, $data);

This is wrong. You must call it like "theme('table', $header, $data);". Calling it directly means that it can not be overriden by a theme, which defeats the purpose of this theme layer completely.

Bad theme_table call

Yes, I've learned this lately, thanks. I'll correct this article shortly.

Usage in Drupal 7

To use this in Drupal 7
change
return theme('table', $header, $data);
to
return theme('table', array('header' => $header, 'rows' => $data));

See http://drupal.org/node/156863#comment-5646000

Post new comment