Drupal Forum Topic List Customization

Drupal Forum Topic List Customization

Posted by matt on Mon, 02/07/2007 - 6:46pm in

For a new project I have been working on, I wanted to alter the default Drupal forum topic list, so that instead of displaying the following columns:

TOPIC - REPLIES - CREATED - LAST REPLY

I wanted it to simply display:

TOPIC - REPLIES

I soon found out that I would need to customize Drupal forum at the theme level, by creating a forum_topic_list.tpl.php file in my theme directory, and adding a snippet of code to template.php, which was also in my theme directory.

For those wondering how to do this, here goes.

Drupal 5.x and Forum Customization

Step 1

Add the following PHP snippet to your template.php theme file.

/**
* This snippet tells Drupal to override the forum_topic_list function
* and load a custom forum_topic_list.tpl.php layout file
* in the theme folder
*/
function phptemplate_forum_topic_list($tid, $topics, $sortby, $forum_per_page) {
return _phptemplate_callback('forum_topic_list', array('tid' => $tid, 'topics' =>$topics, 'sortby' =>$sortby, 'forum_per_page' =>$forum_per_page));
}

Step 2

Create a forum_topic_list.tpl.php file in your theme directory, which contains the following PHP code (don't forget to add an opening PHP tag:

/**
* Format the topic listing.
*
* @ingroup themeable
*/
//function theme_forum_topic_list($tid, $topics, $sortby, $forum_per_page) {
global $forum_topic_list_header;

$forum_topic_list_header = array(
array('data' => ' ', 'field' => NULL),
array('data' => t('Topic'), 'field' => 'n.title'),
array('data' => t('Replies'), 'field' => 'l.comment_count'),
// array('data' => t('Created'), 'field' => 'n.created'),
// array('data' => t('Last reply'), 'field' => 'l.last_comment_timestamp'),
);

if ($topics) {

foreach ($topics as $topic) {
// folder is new if topic is new or there are new comments since last visit
if ($topic->tid != $tid) {
$rows[] = array(
array('data' => theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky), 'class' => 'icon'),
array('data' => check_plain($topic->title), 'class' => 'title'),
array('data' => l(t('This topic has been moved'), "forum/$topic->tid"), 'colspan' => '3')
);
}
else {
$rows[] = array(
array('data' => theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky), 'class' => 'icon'),
array('data' => l($topic->title, "node/$topic->nid"), 'class' => 'topic'),
array('data' => $topic->num_comments . ($topic->new_replies ? ''. l(format_plural($topic->new_replies, '1 new', '@count new'), "node/$topic->nid", NULL, NULL, 'new') : ''), 'class' => 'replies'),
//array('data' => _forum_format($topic), 'class' => 'created'),
//array('data' => _forum_format(isset($topic->last_reply) ? $topic->last_reply : NULL), 'class' => 'last-reply')
);
}
}
}

$output = theme('table', $forum_topic_list_header, $rows);
$output .= theme('pager', NULL, $forum_per_page, 0);

print theme('table', $forum_topic_list_header, $rows);

**********

Now I also noticed that somewhere along the way I had lost the pagination along the bottom of the forum topics list pages (if the number of forum topics was over the max per page). This effectivily was hiding a lot of my forum threads from my users.

To get around this I placed the following code right after the last row of code written above...

print theme('pager');

I now have the pagination "1,2 Next Last" etc at the bottom of each forum topic list once more.

Now, in theory, you should have a Drupal 5.x site with a customized forum topic list - which only displays the topic's title and the number of replies.

I make no guarantees that this is the correct way to do things, but it seems to be working ok on my site. I'd welcome any feedback or corrections!

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
We need to know if you're a human :)
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.