Example 1: Define and Display a Table

This example simply defines a table using a builder function. The data for this table has been hardcoded, but other examples will model pulling data from the database for display. The table as defined below may be displayed on any page in your Drupal site with an Input format of PHP with the following code:

<?php
print tapir_get_table('tapirtest_example_table_1', $null);
?>

This example also shows you how to create your own page in the module to display the table. It works in much the same way. You're just returning the output of the function instead of printing it. You also have to use Drupal's hook_menu to define the menu callback.

Example: view page

Code:

<?php
function tapirtest_menu($may_cache) {
  if (
$may_cache) {
   
$items[] = array(
     
'path' => 'tapirtest/example_1',
     
'title' => t('Example 1: Define and Display'),
     
'callback' => 'tapirtest_example_1',
     
'access' => user_access('access content'),
     
'type' => MENU_CALLBACK,
    );
  }
  return
$items;
}

function

tapirtest_example_1() {
 
$output = '<p>'. tapir_get_table('tapirtest_example_table_1', $null) .'</p>';
  return
$output;
}

function

tapirtest_example_table_1($op) {
  switch (
$op) {
    case
'fields':
     
$fields[] = array('name' => 'song', 'title' => t('Song'), 'weight' => 0,
                       
'enabled' => TRUE);
     
$fields[] = array('name' => 'artist', 'title' => t('Artist'), 'weight' => 1,
                       
'enabled' => TRUE);
     
$fields[] = array('name' => 'album', 'title' => t('Album'), 'weight' => 2,
                       
'enabled' => TRUE);
      return
$fields;
    case
'data':
     
$data['song'] = array('All for You', 'Faint', 'Breach Birth');
     
$data['artist'] = array('Our Lady Peace', 'Linkin Park', 'Chevelle');
     
$data['album'] = array('Gravity', 'Meteora', 'This Type of Thinking');
      return
$data;
  }
}
?>