to theme the comments in my forum i'd like to create a comment-forum.tpl.php. However it seems that this does not work like my node-forum.tpl.php does ( it does not seem to work at all ) how can i use my own template for the comments? I can clearly see that the comment template is taken from the phptemplate engines directory .. but i dont necessarily want comments to my blogs or stories to follow the same template as comments to my forums .. what to do?

Comments

pacheco’s picture

Assuming you have an "template.php" file in your theme folder:


function _is_forum($arg = NULL) {
  static $is_forum = FALSE;
  if ($arg) {
    $is_forum = $arg;
  }
  return $is_forum;
}

function _phptemplate_variables($hook, $vars) {
  static $is_forum; 

  if (!isset($is_forum)) {
    $is_forum = (arg(2) == 'forum');
    _is_forum($is_forum);
  }

  switch ($hook) {
    case 'page':
      break;
    case 'comment' :
      if ($is_forum) $variables['template_file'] = 'comment_forum';      
      break;
    case 'node' :      
      break;
  }

  return $variables;
}

them you need to create the new comment's template file called "comment_forum.tpl.php".

you can pass another variables to this new template or the other ones using the "switch" estatement.

chiggsy’s picture

thanks a lot .. this is just what i needed...

dreed47’s picture

Is this approach intented to work on 4.6 or 4.7 or both? It doesn't seem to work on 4.6

lenart’s picture

I played with the code pacheco wrote and it didn't work for me on Drupal 5. I managed to get it to work and here's the code for template.php:

function _is_forum($arg = NULL) {
  static $is_forum = FALSE;
  if ($arg) {
    $is_forum = $arg;
  }
  return $is_forum;
}

function _phptemplate_variables($hook, $vars) {
  static $is_forum;

  if (!isset($is_forum)) {
	$is_forum = ($vars['type'] == 'forum');
    _is_forum($is_forum);
  }

  switch ($hook) {
    case 'page':
      break;
    case 'comment' :
      if ($is_forum) $variables['template_file'] = 'comment_forum';
      break;
    case 'node' :     
      break;
  }

  return $variables;
}

You then create node-forum.tpl.php to theme posts and comment_forum.tpl.php to theme replies to that post.

Hope I helped somebody :)

sepeck’s picture

If you could post this as a handbook page here: http://drupal.org/node/45471 it would be more findable by folks . :)

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

kriskd’s picture

I played with the code pacheco wrote and it didn't work for me on Drupal 5. I managed to get it to work and here's the code for template.php:

Unfortunately this code broke my site although it's probably my fault since I'm not a coder.

I'm getting the following error:

Cannot redeclare _phptemplate_variables()

So, I'm guessing there is a conflict with something else in my template.php. Any tips for fixing this?

Thanks for the help!

vm’s picture

scan your template .php does you have two ?

_phptemplate_variables()

if so they must be merged. More information on merging can be found in the handbooks, or place a link to your template.php here using a service like pastebin.com

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

kriskd’s picture

Thanks again, VeryMisunderstood! I found this page and it took care of my problem on the first try!

kriskd’s picture

I got a suggestion from this thread to add my theme name to function _phptemplate_variables. So that line now looks like this:

function _phptemplate_variables_mythemename($hook, $vars) {

I no longer get any big scary errors, but my forum isn't using my comment_forum.tpl.php file either. It's still using comment.tpl.php. The difference between the two is I'm trying to get user pictures to only display in forum comments and not other comments on the site.

Thanks for any help that can be provided.

vm’s picture

where mythemename is the actual name of the theme you are using .... yes ?

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )