Hi,

I reported a problem here: http://drupal.org/node/160624, but closed the issue after reading: http://drupal.org/node/112514, however, I'm not sure if that option fits in with the way comments works at the moment. The supplied patch fixes the 'hook_link_alter gets called for single comment views but not for multiple comment views' problem with only minor code modifications.

The problem and solution are detailed below:
Comment Link alter inconsistency.

in comment module, function comment render, hook_link_alter is called if the comment being rendered is a single comment ($cid has a value). Lines 960 to 967 are:

        $links = module_invoke_all('link', 'comment', $comment, 1);

        foreach (module_implements('link_alter') as $module) {
          $function = $module .'_link_alter';
          $function($node, $links);
        }

        $output .= theme('comment_view', $comment, $links);

however, in the 'multiple comment view' section of the same function (970-1056), depending on flat/threaded/expanded/collapsed comments, various functions get called, such as

          $output .= theme('comment_flat_collapsed', $comment);
 ...         $output .= theme('comment_flat_expanded', $comment);

In the corresponding theme functions (1793-1812), module_invoke_all('link', 'comment', $comment, 0) is called for the 'expanded' comments, but not the collapsed ones (i guess this is intentional), however, hook_link_alter is not called as it was for the single comments.

Since hook_link_alter requires a 'node' parameter, it would make sense placing link_alter code in the 'comment_render' function (as it is for single comments) before the theme('comment'... functions are called and modifying these functions to also accept the $links parameter, as the theme('comment... call for single comments does.

In this patch, the hook_link_alter code is placed within the 'multiple comment view' code and the resultant $links are passed to the theme as theme('comment_[type], $comment, $links). This patch fires the link_alter code in the appropriate place, and makes the theme('comment_[type] calls consistent with each other (i.e. 'comment_type', $comment, $links). This also means that link_alter will be passed to 'collapsed' comments though, and I don't know if this is desirable.

This patch was needed by a module where I needed greater control over the links on multiple comment forms on the same page. The patch applies against drupal 5.x.dev (comment module v 1.520.2.6, also in drupal 5.2). If this is a 'goer' I'll roll one for 6.x too.

CommentFileSizeAuthor
comment-link-alter.patch2.76 KBsubspaceeddy

Comments

markus_petrux’s picture

Version: 5.x-dev » 6.x-dev

We're going to render comments in "Flat list - expanded" mode and also found this problem. We need to remove the 'comment_reply' link so comments are only attached to nodes.

Changing to 6.x-dev so it doesn't get missed. The fix is trivial, though it probably needs to be rewritten.

In the meantime (we're working on 5.x right now), I'm solving this by overrding a couple of themable functions, theme_comment_flat_expanded and theme_comment_thread_expanded, like this:

function phptemplate_comment_flat_expanded($comment) {
  $links = module_invoke_all('link', 'comment', $comment, 0);
  foreach (module_implements('link_alter') as $module) {
    $function = $module .'_link_alter';
    $function($node, $links);
  }
  return theme('comment_view', $comment, $links);
}
function phptemplate_comment_thread_expanded($comment) {
  $output = '';
  $links = module_invoke_all('link', 'comment', $comment, 0);
  foreach (module_implements('link_alter') as $module) {
    $function = $module .'_link_alter';
    $function($node, $links);
  }
  $output .= theme('comment_view', $comment, $links);
  return $output;
}

Cheers

catch’s picture

Status: Needs review » Needs work
mpg’s picture

Priority: Normal » Critical

there is a lot going on here for a beginner to understand...

I am a beginner to php and drupal though i have web development experience i do not know where to begin. I am unfamiliar with overriding functions and implementing patches let alone where and how to implement the hook_link_alter function.

My goal is to change the link titles for comments based on if it is a blog, forum or review entry.

Where can i find a step be step tutorial to learn how to accomplish this?

thank you kindly.

gábor hojtsy’s picture

Version: 6.x-dev » 7.x-dev
Category: bug » feature
Priority: Critical » Normal

@mpg: Please do not hijack existing reports with your support question. A good book on Drupal development (and thus implementing hooks) is Pro Drupal Development, but you can also pick up a lot in the forums.

@subspaceeddy: hook_link_alter() is documented to apply to nodes, not comments: http://api.drupal.org/api/function/hook_link_alter/6 If you are looking to expands its scope, Drupal 7 is a good place to do it. Such a change would not be accepted to Drupal 7 due to being a new feature / redefinition of this hook.

catch’s picture

Status: Needs work » Closed (duplicate)

I meant to mark this duplicate in #2 but it seems I slipped - the other issue is where the main discussion is happening.

Note that in Drupal 7, there's no hook_link() for nodes any more - links are added in hook_nodeapi_view() - with some luck, hook_link() for comments will go the same way, but that discussion should happen in #112514: hook_link_alter doesn't work for comment links.