description

These snippets allow you to override the default forum list layout - which is the main /forum overview page that lists all the forum containers and forums.

If you want to load a custom page-forum.tpl.php to control the entire page layout, click through to the Customising the full page layout and sections based on path handbook page.

usage

This simple example overrides the default and displays a forum list as illustrated by the screenshot below.

Only local images are allowed.
(screenshot reduced in size so it fits in the Drupal handbook page. A larger screen shot is attached to this book page)

Browser/validation check

Only local images are allowed.

 

Only local images are allowed.

The CSS style sheet sheet and HTML output for this override validates and has been tested on the following browser for consistency (i.e. it looks the same across browsers):

  • Internet Explorer 6.0
  • FIREFOX 2.0
  • OPERA 9.2

If you have tested this on other browsers and can confirm consistency/report problems, please post a comment or if you have permission, edit this book page to keep it updated.

Step 1 of 3

  1. In a text editor, create a new file called template.php and copy the following snippet into the file. If a template.php file already exists, simply add the snippet remembering to omit the opening and closing PHP tags (i.e. <?php and ?>)
  2. Upload your new or edited template.php to your active theme folder
<?php
/**
* This snippet tells Drupal to override the forum_list function
* and load a custom forum_list.tpl.php layout file
* in the theme folder
*/
function phptemplate_forum_list($forums, $parents, $tid) {
return _phptemplate_callback('forum_list', array('forums' => $forums, 'parents' =>$parents, 'tid' =>$tid));
}
?>

Step 2 of 3

  1. In a text editor, create a new file called forum_list.tpl.php and copy the following snippet into the file. This time you want to keep the opening PHP tag (i.e. <?php)
  2. Upload your new forum_list.tpl.php file to your active theme folder and your new layouts will take effect automatically
  3. Add in some example forums and containers and edit the forum_list.tpl.php file to suit being careful to not break the php.
<?php 
 /**
 * This snippet generates the forum_list layout.
 * This works with Drupal 4.7 and Drupal 5.x
 * For use with a custom forum_list.tpl.php file.
 *
 */

  global $user; // checks the current user so later the snippet can determine what permissions they have
  
  if ($forums) { // check if there is forums.
  
  //  remove the default header because we're going to add it in under each container name
  //  $header = array(t('Forum'), t('Topics'), t('Posts'), t('Last post'));
  
    foreach ($forums as $key=>$forum) { // this is the start of the forum list routine that generates the containers and forum list.
      if ($forum->container) { 
        $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
        $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid/container") ."</div>\n";
        if ($forum->description) {
          $description .= " <div class=\"description\">$forum->description</div>\n";
        }
        $description .= "</div>\n";
        $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => 4));
        $rows[] = array(
        array('data' => t('Subject'), 'class' => 'f-subject'), // this sets the column titls and style sheet class names
        array('data' => t('Topics'), 'class' => 'f-topics'),
        array('data' => t('Posts'),'class' => 'f-posts'),
        array('data' => t('Last post'),'class' => 'f-last-reply')
        );
      }
      else {
        $forum->old_topics = _forum_topics_unread($forum->tid, $user->uid);
        if ($user->uid) {
          $new_topics = $forum->num_topics - $forum->old_topics;
        }
        else {
          $new_topics = 0;
        }
        $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
        $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";
        if ($forum->description) {
          $description .= " <div class=\"description\">$forum->description</div>\n";
        }
        $description .= "</div>\n";
        $rows[] = array(
          array('data' => $description, 'class' => 'forum'),
          array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(format_plural($new_topics, '1 new', '@count new'), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'),
          array('data' => $forum->num_posts, 'class' => 'posts'),
          array('data' => _forum_format($forum->last_post), 'class' => 'last-reply'));
      }
    }
    /**
     * Only set the table header if page is a container with listing of forums
     */
    if(in_array(arg(1), variable_get('forum_containers', array()))){
            // reverse array
            $nrows = array_reverse($rows,true);
            //get the container description
            $container = taxonomy_get_term(arg(1));
            //add to output
            $rows = $nrows;
            $rows[] = array(
                array('data' => t('Subject'), 'class' => 'f-subject'),
                array('data' => t('Topics'), 'class' => 'f-topics'),
                array('data' => t('Posts'),'class' => 'f-posts'),
                array('data' => t('Last post'),'class' => 'f-last-reply')
                );
            $rows[] = array(array('data' => $container->description, 'class' => 'container', 'colspan' => 4));
            //reverse again to output
            $nrows = array_reverse($rows,true);
            $rows = $nrows;
        }
    print theme('table', $header, $rows);
    
  }
?>

Step 3 of 3

Below is the style sheet snippet for the above example.

/* $Id: forum-style.css,v 1.13 2007/06/25 14:00:00 Dublin Drupaller Exp $ */
/* Forum list style */
/* Please note that you maybe inheriting styles from the modules/forum/forum.css style sheet */

/* the main Forum table */
#forum table {width:100%; border:1px solid #eaeaea;}
#forum td { padding: .6em .6em .6em .6em;}
#forum .odd {background:#ffffff;color:#808080;}
#forum .even {background:#f1f1f1;color:#808080;}
/* end of main Forum table styles */

/* the container styles */
#forum .container {background-color:black; color:#fff; font-weight:normal; font-size:1.1em ;}
#forum .container a {color:#fff;font-weight:bold;}
#forum .container .name {float:left;margin:0em; padding:5px 12px; font-size:1.6em;}
#forum .container .description {float:left; padding-top:4px;color:#fff;}
/* end of container styles */

/* the subject, topics, number of posts and last reply header styles */
#forum td.f-subject, #forum td.f-topics, #forum td.f-posts, #forum td.f-last-reply{background-color:#eaeaea;height: 1.5em;font-weight:bold;}
/* end of subject, topics, number of posts and last reply header styles */


/* forum listing styles  */
#forum td.forum {margin: 0em; padding: .9em;}
#forum td.forum div {margin: 0em; padding: 0em;}
#forum .forum .name a {color:#454545;font-size: 1.2em ; }
#forum .forum .description {color:#454545;font-size: 1.1em ;}
#forum .topics {border-left:1px solid #eaeaea;text-align:center;font-weight:normal;font-size: 14px ;}
#forum .last-reply {border-left:1px solid #eaeaea; font-weight:normal; padding:0 6px;}
#forum .post_date {border-left:1px solid #eaeaea;position:relative;top:-3px;text-align:right;}
#forum .posts {border-left:1px solid #eaeaea;text-align:center;font-weight:normal; font-size:14px ;}
/* end of forum listing styles  */

/* end of forum list style */

notes

  • I'll update this book page soon with line-by-line comments so it's easier for non-programmes to follow what's going on as the forum_list is being generated, making it easier for designers to get stuck in without getting bamboozled by the php code.
  • I like the idea of other Drupallers adding their own custom forum_list.tpl.php snippet + stylesheet snippet+ screenshot as CHILD PAGES to this handbook page, so we can share layouts in a more intuitive fashion.
  • If you are adding a CHILD PAGE to this page, please follow the same format as this page to maintain consistency.

Comments

Greg Go’s picture

I'm not sure when this changed, but in my drupal 5.2 install, _forum_topics_unread() returns the number of new topics. I altered

this code in step 2 of the original post :

        $forum->old_topics = _forum_topics_unread($forum->tid, $user->uid);
        if ($user->uid) {
          $new_topics = $forum->num_topics - $forum->old_topics;
        }
        else {
          $new_topics = 0;
        }

... to...

        $new_topics = _forum_topics_unread($forum->tid, $user->uid);
Richard_’s picture

In step 3 there is no mention where to place forum-style.css

I had to replace forum.css with forum-style.css content to make it work.

I tried to create forum-style.css file and place it either in theme directory or forum module directory but css didnt work.

chemdata’s picture

Is there a version of these changes that can be used with drupal 6? Thanks.

Dublin Drupaller’s picture

anyone know how we can edit this HB page?

I'm trying to change the links on the i mages referenced in the page.

cheers

dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

silverwing’s picture

The input format is set to Documentation. So you can either create an issue in the Documentation issue queue or just apply to be a documentation admin.