Default forum images for forum topics and forum posts

Show a default image for top level forum containers and its sub level topics. Be able to override the default image shown for each sub forum topics.

The usability for a non-techie to assign and modify default images is clean and easy. For a default image, add the "Default Forum Image" node. If a sub topic image overrides the default, then when creating the Forum Topic node, choose the image from your computer that will be populated into the imagefield of the node.

However, on the coding side, this is a bit of a hack, I am sure it can be done in a better way. Comments on improvement appreciated.

Step 1 of 4: Create a custom content type for default forum images

  • Enable cck and imagefield modules
  • Create a custom content type called "Default Forum Image": /admin/content/types/add
  • Add a field to the custom content type type 'image' (imagefield)
  • Delete the Body Field Label if description of image not needed
  • Apply the Forums vocabulary to the "Default Forum Image" content type: /admin/content/taxonomy
  • Notify the client, that they have to add maximum one "Default Forum Image" node per forum

Step 2 of 4: Add imagefield to the forum topic content type

  • Add imagefield to the forum topic content type /admin/content/types/forum/add_field

Step 3 of 4: Modify template.php and page.tpl.php

  • Modify template.php function _phptemplate_variables() under the 'page' part of the switch statement, populate a themed image in $vars['default_image']
      if (arg(0) == 'forum' && is_numeric(arg(1))) {
        /** 
         * Get the image from table content_type_forum_image, field field_image_1_alt
         */
        $sql  = 'SELECT tn.nid FROM term_node tn JOIN node n ON tn.nid = n.nid ';
        $sql .= 'JOIN content_type_forum_image fi ON tn.nid = fi.nid ';
        $sql .= 'WHERE n.status = 1 AND tn.tid = ' . arg(1) . ' LIMIT 0,1';
        $default_image_nid  = db_result(db_query($sql));
        if (! $default_image_nid) {
          // Look for the parent container image
          $sql  = 'SELECT tn.nid FROM term_hierarchy th ';
          $sql .= 'JOIN term_node tn ON tn.tid = th.parent ';
          $sql .= 'JOIN node n ON tn.nid = n.nid ';
          $sql .= 'JOIN content_type_forum_image fi ON tn.nid = fi.nid ';
          $sql .= 'WHERE n.status = 1 AND th.tid = ' . arg(1) . ' LIMIT 0,1';
          $default_image_nid  = db_result(db_query($sql));
        }
        $default_image_node = node_load(array('nid' => $default_image_nid));
        $vars['default_image'] = theme('image', variable_get('file_directory_path', 'files') . '/imagecache/forum/' . $default_image_node->field_image_1[0]['filepath'], $default_image_node->title);
      } elseif ($vars['node']->type == 'forum') {
        if (isset($vars['node']->field_image_0)) {
          $vars['default_image'] = theme('image', variable_get('file_directory_path', 'files') . '/imagecache/forum/' . $vars['node']->field_image_0[0]['filepath']);
        } else {
          // Get the default forum taxonomy image
          foreach ($vars['node']->taxonomy as $key=>$term_data) {
            if ($term_data->vid == 1) {      //This is the forum vocabulary
              /** 
               * Get the image from table content_type_forum_image, field field_image_1_alt
               */
              $sql  = 'SELECT tn.nid FROM term_node tn JOIN node n ON tn.nid = n.nid ';
              $sql .= 'JOIN content_type_forum_image fi ON tn.nid = fi.nid ';
              $sql .= 'WHERE n.status = 1 AND tn.tid = ' . $term_data->tid . ' LIMIT 0,1';
              $default_image_nid  = db_result(db_query($sql));
              if (! $default_image_nid) {
                // Look for the parent container image
                $sql  = 'SELECT tn.nid FROM term_hierarchy th ';
                $sql .= 'JOIN term_node tn ON tn.tid = th.parent ';
                $sql .= 'JOIN node n ON tn.nid = n.nid ';
                $sql .= 'JOIN content_type_forum_image fi ON tn.nid = fi.nid ';
                $sql .= 'WHERE n.status = 1 AND th.tid = ' . $term_data->tid . ' LIMIT 0,1';
                $default_image_nid  = db_result(db_query($sql));
                }
              break;  // already found the forum vocabulary 
            }
          }
          $default_image_node = node_load(array('nid' => $default_image_nid));
          $vars['default_image'] = theme('image', variable_get('file_directory_path', 'files') . '/imagecache/forum/' . $default_image_node->field_image_1[0]['filepath']);
        }
      }
  • Use the $default_image variable in page.tpl.php
  •     if ($default_image) {
          echo '<div class="forum-default-image">';
          echo $default_image;
          echo '</div>';
        }
    

    Step 4 of 4: move around the display of imagefield of the forum topic

    • copy node.tpl.php to node-forum.tpl.php
    • Modify template.php function _phptemplate_variables under case 'node'
    • Instead of $body display $body_no_image in node-forum.tpl.php
          // used in node-forum.tpl.php, to not show the imagefield cck
          $vars['body_no_image'] = $vars['node']->content['body']['#value'] . $vars['node']->content['forum_navigation']['#value'];
    
    

    Module dependencies

    • CCK
    • imagefield

    Example

    http://community.eharlequin.com/forums/kimani