Search this blog:

Words and works.

How to Override the Theming for a Drupal Module

Here's a little "how to" I recently picked up while working on the Humboldt Community Christian School web site.

The site uses the Event module. The default formatting for the "Upcoming events" block shows the number of days until the event will occur, rather than the date of the event. Not helpful. While it is interesting to know the number of days until the event will occur, visitors checking the date of the event against their own calendar will want to know the date of the event.

Fortunately, the Event module provides some themed functions in the event.theme file that make it fairly easy to change the layout of the block. The function that lays out the events listed in the Upcoming Events block is as follows:


function theme_event_upcoming_item($node) {
  $output = l($node->title, "node/$node->nid", array('title' => $node->title));
  if ((event_get_types('all') + event_get_types('solo')) > 1) {
    $output .= ''. t("($node->typename)") .'';
  }
  $output .= '('. $node->timeleft .')';
  return $output;
}

The template I'm using for the site is based on the PHPTemplate theme engine, therefore the template.php file in the theme directory is the place to put logic that overrides the logic defined in the module. Here's the code I put in that file (lifted from here):


function hccs_event_upcoming_item($node) {
  // Initialize the content to nothing
  $output = "";
  // Put in the date, stripped of any tags/class to prevent putting it on a separate line
  $output .= _event_date('m-d-Y', $node->event_start) . " " . $node->tz. " ";
  // Put in the event title with link
  $output .= l($node->title, "node/$node->nid", array('title' => $node->title));
  // Return the output back to Drupal for display
  return $output;
}

Note that the function name changed. I replaced the word "theme" with the name of my theme, which is "hccs". Now Drupal uses the logic in my custom layout function rather than the standard layout of the Event module.

Using this method to override the layout is preferred over directly changing the code in the event.theme file since installing an upgrade of the Event module could cause your custom changes to be lost.

Tags: drupal | Permalink

© 2010 Evenhouse Consulting, Inc.

A Django site.