Main topic described: Displaying content

So far we have our working block and a settings page. The block displays a maximum number of links. However, there may be more links than the maximum we show. So, let's create a page that lists all the content that was created a week ago.


function onthisdate_all() {}

We're going to use much of the code from the block function. We'll write this ExtremeProgramming style, and duplicate the code. If we need to use it in a third place, we'll refactor it into a separate function. For now, copy the code to the new function onthisdate_all(). In this case, we could have used whatever name we wanted, as 'all' is not a Drupal hook.

If you want to call this function from another module, use the standard naming scheme we've been using: modulename_action. It can be called using the function module_invoke function. If you want the function to remain private (because, say, it's merely a helper function in your module) and easily accessible by only your module, prefix the function name with an underscore. We want the former.


function onthisdate_all() {
  // content variable that will be returned for display
  $page_content = '';

  // Get today's date
  $today = getdate();

  // calculate midnight one week ago
  $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $today['year']);

  // we want items that occur only on the day in question,
  // so calculate 1 day
  $end_time = $start_time + 86400;
  // 60 * 60 * 24 = 86400 seconds in a day

  $query = "SELECT nid, title, created FROM {node} WHERE created >= %d AND created <= %d";

  // get the links (no range limit here)
  $queryResult =  db_query($query, $start_time, $end_time);
  while ($links = db_fetch_object($queryResult)) {
    $page_content .= l($links->title, 'node/'.$links->nid).'<br />';
  }
  ...
}

We have the page content at this point. As noted before, we're including layout in the code, and we ought to make the output "themeable" (note: the guide linked to there is for Drupal 6! Most of it also applies to Drupal 5, except that you do not have to use hook_theme() to register your theme functions, and you should use the "theme functions" method rather than the "template files" method to define the default behavior). This is bad, and should be avoided. It is, however, beyond the scope of this tutorial, so for now, we'll just include the formatting in our content.

The rest of our function checks to see if there is content and lets the user know. This is preferable to showing an empty or blank page, which may confuse the user.

Note that we simply return the content, and Drupal displays it within a themed page.


function onthisdate_all() {

  ...

  // check to see if there was any content before
  // setting up the block
  if ($page_content == '') {
    // no content from a week ago, let the user know
    $page_content = "No events occurred on this site on this date in history.";
  }

  return $page_content;
}

Even though we have this function that will output links to the content generated a week ago, we haven't specified what URL will cause this page to render. We'll do that next.