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. This is a three-step process, which we'll describe on this tutorial page and the next two pages.

The first step, described on this page, is to add a new function to our onthisdate.module file that will output a complete list of all the content that was created a week ago. We'll name this function onthisdate_all() (we could choose another name, but it must start with "onthisdate_").

A quick note on function naming conventions in Drupal: If you are creating a function that you intend to be strictly private (i.e. no other module should rely on it being a stable function or call it), start the function name with "_your_module_name_". If your function is public (i.e. it would be OK for another module to call it, and you don't intend to change its argument signature or behavior often), start the function name with "your_module_name_". If you are implementing a Drupal hook, you must always name the function "your_module_name_hookname". Likewise, if you are not implementing a Drupal hook, check the hooks to be sure you have not accidentally picked a function name matching a hook.

So, back to the new onthisdate_all() function in the onthisdate.module file. It is basically going to reproduce the original version of our block, from before we put in the option to limit the block to a certain number of links (see Generating the block content earlier in the tutorial), and we want the function to return the HTML content for the page. Note that we don't have to worry about HTML headers, page titles, menus, footers, etc. We only have to generate the content section of the page, and Drupal and our theme will take care of the rest.

So, the basic outline of our function will be:


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

  // ... fill here the page content variable with what we want to show

  // return the page content which we have built
  return $page_content;
}

For the content, here's the part of the function that is essentially copied from the block function:


  // 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 />';
  }

As noted before, we're including layout in the code. This is bad, and should be avoided. An even better practice in Drupal is to make your module's output "themeable". All of that, however, is beyond the scope of this tutorial, so for now, we'll just keep it simple and include the formatting in our content.

Also note that, as in the case of a block, you might want to incorporate a different time range if your site has no content on that particular date one week ago.

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.


  // check to see if there was any content before
  // returning the page
  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.";
  }

So putting each of these pieces above together we get the following complete function:


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 />';
  }

  // check to see if there was any content before
  // returning the page
  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 display this page. We'll do that next.

Comments

pinin4fjords’s picture

Nice tutorial- thank you. But seriously- if themability is that important that you have to keep mentioning it, why don't you integrate it into the example? As a drupal newbie, I'd much rather have this stuff integrated into my concept of the development procedure from the start, rather than having to shift my perceptions immediately after the introduction.

drupeo’s picture

I think the tutorial has been written with php newbies in mind as well as drupal newbies. I read the link to themes three pages ago at http://drupal.org/node/165706 and definitely thought that themes are outside the scope of the tutorial.

Once you have mastered themes I am sure the site authors will not be disinterested in any tutorial you create that includes themes. :)

verbosity’s picture

It's worth pointing out that drupal looks for the $page_content value and knows to use that, changing it to another name wont work.

ibnkhaldun’s picture

Hi.
The variable $page_content is under the function scope. Thus it is not visible out from that function. The name was selected to indicate what the value it halls is.
In this case $page_content does not mean to drupal. It means to readers.