Creating a forum similar to gallery.menalto.com/forum

Last modified: August 26, 2009 - 02:38

I modified the above code to create a forum like http://gallery.menalto.com/forum

Notes

1. Make sure you include the CSS from the previous page http://drupal.org/node/154325 in your style.css file at the bottom.
2. If your forums are not categorized into sub categories then you will perhaps not see much of a change in the main forum page.

template.php

<?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));
}
?>

forum_list.tpl.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.
$container_flag = 0;
$old_depth = 0;
$subforum_flag = 0;
$child_no = 0;
$parent_new = 0;
$parent_posts = 0;
$parent_topics = 0;
$parent_last = 0;
$parent_id = 0;

 
//  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));

     
$container_flag = 1;
      }
      else {

      if (
$container_flag == 1)
      {
       
$limit_depth = 2;
      }
      else
      {
       
$limit_depth = 1;
      }

     
// show only 2 levels
     
if ($forum->depth <= $limit_depth)
      {
       
// for parent forum
       
if ($forum->depth < $limit_depth)
        {
          if (
$subforum_flag == 1)
          {
           
$subforum_flag = 0;
          }
  
           
$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: 0px;\">\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";

         
// does not have subforums
         
$tot_childs = count(taxonomy_get_children($forum->tid));
          if (
$tot_childs == 0)
          {      
           
$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'));
          }
          else
          {  
           
$subforum_flag = 1;
           
$parent_new = $new_topics;
           
$parent_posts = $forum->num_posts;
           
$parent_topics = $forum->num_topics;
           
$parent_last = $forum->last_post;
           
$parent_id = $forum->tid;
          }
        }
        else
// sub forum
         
{
         
$child_no++;
          if (
$old_depth == $forum->depth)
          {
           
$description .= ", ";
          }
        
          if (
$old_depth < $forum->depth)
          {
           
$description .= " <div class=\"subforum\">".t('Subforum').": ";
          }
         
$description .= l($forum->name, "forum/$forum->tid");

          if (
$child_no == $tot_childs)
          {
           
$description .= "</div>";
           
$child_no = 0;
               
$rows[] = array(
                      array(
'data' => $description, 'class' => 'forum'),
                      array(
'data' => $parent_topics . ($parent_new ? '<br />'. l(format_plural($parent_new, '1 new', '@count new'), "forum/$parent_id", NULL, NULL, 'new') : ''), 'class' => 'topics'),
                      array(
'data' => $parent_posts, 'class' => 'posts'),
                      array(
'data' => _forum_format($parent_last), 'class' => 'last-reply'));

          }
          }

       
$old_depth = $forum->depth;
        }
        }  
    }

    print
theme('table', $header, $rows);
 
  }
?>

 
 

Drupal is a registered trademark of Dries Buytaert.