You are here

function template_preprocess_forums in Drupal 6

Same name and namespace in other branches
  1. 8 core/modules/forum/forum.module \template_preprocess_forums()
  2. 7 modules/forum/forum.module \template_preprocess_forums()
  3. 9 core/modules/forum/forum.module \template_preprocess_forums()

Process variables for forums.tpl.php

The $variables array contains the following arguments:

  • $forums
  • $topics
  • $parents
  • $tid
  • $sortby
  • $forum_per_page

See also

forums.tpl.php

File

modules/forum/forum.module, line 658
Enable threaded discussions about general topics.

Code

function template_preprocess_forums(&$variables) {
  global $user;
  $vid = variable_get('forum_nav_vocabulary', '');
  $vocabulary = taxonomy_vocabulary_load($vid);
  $title = !empty($vocabulary->name) ? $vocabulary->name : '';

  // Breadcrumb navigation:
  $breadcrumb[] = l(t('Home'), NULL);
  if ($variables['tid']) {
    $breadcrumb[] = l($vocabulary->name, 'forum');
  }
  if ($variables['parents']) {
    $variables['parents'] = array_reverse($variables['parents']);
    foreach ($variables['parents'] as $p) {
      if ($p->tid == $variables['tid']) {
        $title = $p->name;
      }
      else {
        $breadcrumb[] = l($p->name, 'forum/' . $p->tid);
      }
    }
  }
  drupal_set_breadcrumb($breadcrumb);
  drupal_set_title(check_plain($title));
  if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) {

    // Format the "post new content" links listing.
    $forum_types = array();

    // Loop through all node types for forum vocabulary.
    foreach ($vocabulary->nodes as $type) {

      // Check if the current user has the 'create' permission for this node type.
      if (node_access('create', $type)) {

        // Fetch the "General" name of the content type;
        // Push the link with title and URL to the array.
        $forum_types[$type] = array(
          'title' => t('Post new @node_type', array(
            '@node_type' => node_get_types('name', $type),
          )),
          'href' => 'node/add/' . str_replace('_', '-', $type) . '/' . $variables['tid'],
        );
      }
    }
    if (empty($forum_types)) {

      // The user is logged-in; but denied access to create any new forum content type.
      if ($user->uid) {
        $forum_types['disallowed'] = array(
          'title' => t('You are not allowed to post new content in the forum.'),
        );
      }
      else {
        $forum_types['login'] = array(
          'title' => t('<a href="@login">Login</a> to post new content in the forum.', array(
            '@login' => url('user/login', array(
              'query' => drupal_get_destination(),
            )),
          )),
          'html' => TRUE,
        );
      }
    }
    $variables['links'] = $forum_types;
    if (!empty($variables['forums'])) {
      $variables['forums'] = theme('forum_list', $variables['forums'], $variables['parents'], $variables['tid']);
    }
    else {
      $variables['forums'] = '';
    }
    if ($variables['tid'] && !in_array($variables['tid'], variable_get('forum_containers', array()))) {
      $variables['topics'] = theme('forum_topic_list', $variables['tid'], $variables['topics'], $variables['sortby'], $variables['forum_per_page']);
      drupal_add_feed(url('taxonomy/term/' . $variables['tid'] . '/0/feed'), 'RSS - ' . $title);
    }
    else {
      $variables['topics'] = '';
    }

    // Provide separate template suggestions based on what's being output. Topic id is also accounted for.
    // Check both variables to be safe then the inverse. Forums with topic ID's take precedence.
    if ($variables['forums'] && !$variables['topics']) {
      $variables['template_files'][] = 'forums-containers';
      $variables['template_files'][] = 'forums-' . $variables['tid'];
      $variables['template_files'][] = 'forums-containers-' . $variables['tid'];
    }
    elseif (!$variables['forums'] && $variables['topics']) {
      $variables['template_files'][] = 'forums-topics';
      $variables['template_files'][] = 'forums-' . $variables['tid'];
      $variables['template_files'][] = 'forums-topics-' . $variables['tid'];
    }
    else {
      $variables['template_files'][] = 'forums-' . $variables['tid'];
    }
  }
  else {
    drupal_set_title(t('No forums defined'));
    $variables['links'] = array();
    $variables['forums'] = '';
    $variables['topics'] = '';
  }
}