I've got a Drupal 5.1 install, using the Zen theme, and I'm a PHP-noob.

With this theme, each comment includes a title which functions as the permalink for the comment.

For example, a comment title would link directly to the comment with (http://www.example.com/2007/02/15/blog-entry#comment-49908)

What I'd like to do to is eliminate the displaying of the comment title entirely, but if I do that (by editing comment.tpl.php) then I no longer have access to the comment permalinks.

Is there a way to add a comment permalink to the links section of each comment?

My comment.tpl.php file is:

<div class="comment<?php if ($comment->status == COMMENT_NOT_PUBLISHED) print ' comment-unpublished'; ?>">
<?php if ($new != '') { ?><span class="new"><?php print $new; ?></span><?php } ?>
<h3 class="title"><?php print $title; ?></h3>
<?php if ($picture) print $picture; ?>
    <div class="submitted"><?php print t('On ') . format_date($comment->timestamp, 'custom', 'F jS, Y \\a\t h:i A'); ?> <?php print theme('username', $comment) . t(' says:'); ?></div>
    <div class="content"><?php print $content; ?></div>
    <div class="links"><?php print $links; ?></div>
</div>

Any advice that you could give would be greatly appreciated.

Comments

dldege’s picture

You can turn off the title display in the comment settings.

Comment subject field:
*Disabled

in Zen's template.php you can find this bit of code which check this.

      // if comment subjects are disabled, don't display 'em
      if (variable_get('comment_subject_field', 1) == 0) {
        $vars['title'] = '';
      }

you could disable the subject as per above then add these lines above the code I just showed

      $vars['title'] = str_replace($vars['comment']->subject, "permalink", $vars['title']);
      $vars['links'] = str_replace("</ul>", "<li class='permalink'>" . $vars['title'] . "</li></ul>", $vars['links']);

This will put the permalink into the links variable.

Another option is to write some module code that handled hook_link and or hook_link_alter to get this out of the theme if you wanted.

dLd

gordyhulten’s picture

Thanks - I'm not seeing this:

<?php
      // if comment subjects are disabled, don't display 'em
      if (variable_get('comment_subject_field', 1) == 0) {
        $vars['title'] = '';
      }
?>

in Zen's template.php file. I just re-downloaded and looked in the original, too.

But it is included in the latest development version of zen.

Any advice on how to upgrade a theme? :-)

dldege’s picture

I have the DRUPAL-5 revision from cvs (-r DRUPAL-5). You could try using CVS to update your copy (instead of downloading the zip file). http://drupal.org/node/321.

Are you making any custom modules? Here is the code to do the same thing in hook_link() which might be easier then messing with your theme if you already have a lot invested in the current version of Zen you are using. (The latest version of Zen is a lot better though - and also works with the css caching in Drupal 5 which did not work in some previous versions).

function mymodule_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();
  if ($type == 'comment')
    $links['permalink'] = array('title' => 'permalink', 'href' => 'node/'.$node->nid, 'fragment' => 'comment-' . $node->cid);
  
  return $links;
}

Edit: I had a # in the 'fragment' => part which was not needed and resulted in ## in the permalink.
dLd

dldege’s picture

also, the two lines of template.php code to add the permalink should work in your version of the theme if you can find the right spot to put it - somewhere in _phptemplate_variables for the 'comment' case of the switch (or add a 'comment' case if not there).

dLd

gordyhulten’s picture

The code works great with the updated version of the Zen theme.

A noob question: how can I get the permalink to always read "permalink" instead of using the title of the comment?

Thanks again.

dldege’s picture

The code should be using 'permalink' instead of the title.

      $vars['title'] = str_replace($vars['comment']->subject, "permalink", $vars['title']);
      $vars['links'] = str_replace("</ul>", "<li class='permalink'>" . $vars['title'] . "</li></ul>", $vars['links']);

the first line replaces the comment subject with 'permalink'. The second line add it to the links. What are you seeing?

Paste your code if necessary.

Thanks,
dLd

gordyhulten’s picture

Here's my code:

$vars['comment_classes'] = implode(' ', $comment_classes);

      // adding permalinks to comments with disabled subjects
	  $vars['title'] = str_replace($vars['comment']->subject, "permalink", $vars['title']);
	  $vars['links'] = str_replace("</ul>", "<li class='permalink'>" . $vars['title'] . "</li></ul>", $vars['links']);
      	  
      // if comment subjects are disabled, don't display 'em
      if (variable_get('comment_subject_field', 1) == 0) {
		$vars['title'] = '';
	  }      
      break;

What I'm seeing is that, even with the comment titles disabled, some comment permalinks are still being created as the title of the comment, which in this case is always the first few words of the comment.

Here's an example - scroll down to see the permalinks for some of the comments. For some comments, the permalink is titled "permalink." For others, it's the truncated first line/post title. It's doing it for some comments, but not for all of them. Viewing the source didn't give me any clues, but I'm not very good at this stuff.

I've tried moving those two lines of code around a bit - inside the "if" statement, above it, below it, but while some placements make the permalink go away entirely, none fix the titling mistakes.

Thanks for your help.

gordyhulten’s picture

I just deleted this line from my comment.tpl.php, and that didn't fix the problem either:

<?php if ($title) { ?><h3 class="title"><?php print $title; ?></h3><?php } ?>

Deleting it appears to have had no effect.

dldege’s picture

Hi, a quick glance I noted that all the failure cases have double quotes in them -- it looks like my quick and dirty str_replace() doesn't like quotes in the original title. Here is a better solution that doesn't use a str_replace for building the permalink link.

    $vars['links'] = str_replace("</ul>", "<li class='permalink'>" . l('permalink', 'node/' . $node->nid, NULL, NULL, 'comment-' . $vars['comment']->cid) . "</li></ul>", $vars['links']);

Put this line in instead of the two lines I previously suggested, that is replace

  $vars['title'] = str_replace($vars['comment']->subject, "permalinker", $vars['title']);
  $vars['links'] = str_replace("</ul>", "<li class='permalink'>" . $vars['title'] . "</li></ul>", $vars['links']);

with the above.

It use the l() function to build the permalink (similar to the module hook_link approach) instead of trying to do a string replace on the original href in the title.

dLd

gordyhulten’s picture

Perfect! Thank you!

amitaibu’s picture

Just to make it clear - took me some time to get it going… :)
In order to remove the title and have Permalink appear in the links
in ZEN theme, under template.php, under function _phptemplate_variables have this:

    case 'comment':
      // we load the node object that the current comment is attached to
      $node = node_load($vars['comment']->nid);
      // if the author of this comment is equal to the author of the node, we set a variable
      // then in our theme we can theme this comment differently to stand out
      $vars['author_comment'] = $vars['comment']->uid == $node->uid ? TRUE : FALSE;

      // Change Comment title to Permalink
        $vars['title'] = str_replace($vars['comment']->subject, "permalink", $vars['title']);
        $vars['links'] = str_replace("</ul>", "<li class='permalink'>" . $vars['title'] . "</li></ul>", $vars['links']);

      // if comment subjects are disabled, don't display them
	   if (variable_get('comment_subject_field', 1) == 0) {
	   $vars['title'] = '';
      }

(without the opening and closing (i.e. <?php and ?>)PHP symbol)
---
gizra.com - where cool software developers meet geek fashion designers