3

I am trying to alter the "reply" links added to Nodecomment comments when the nodes are listed beneath the topic. The problem I have is that I want users to be restricted to the inline submit form, but Nodecomment is adding links to the standalone reply page.

While trying to ovveride the links using hook_link_alter(), I ran into an issue with Nodecomment comments are treated much like regular comments in that hook_link_alter() doesn't seem to be run on them. Here is an example:

/**
* Alter module links. 
*
*/
function mymodule_link_alter(&$links, $node) {

    // This is an example of altering links that works as expected
    if (isset($links['comment_add']) && node_access('create', $node)) {
      $links['comment_reply'] = array(
        'title' => t('reply'),
        'title' => t('Link altered'),
        'href' => 'http://example.com',
      );
    }

    // This is an example of altering nodecomment links failing.
    // When rendering a list of topic replies, the comment_reply link
    // will not be altered.
    if (isset($links['comment_reply']) && node_access('create', $node)) {
      $links['comment_reply'] = array(
        'title' => t('reply'),
        'title' => t('Link altered'),
        'href' => 'http://example.com',
      );
    }
}

I am looking for solutions which can (ideally) be packaged in the site's custom module rather than theme.

2 Answers 2

2

Reading the documentation for hook_link_alter() I find the following sentence.

Perform alterations before links on a node or comment are rendered.

The hook implementation you are using is missing the last argument: the definition of the hook given by the documentation is hook_link_alter(&$links, $node, $comment = NULL).

Looking at the code of comment_render(), which is the function that renders all the comments of a node, you can notice that hook_link_alter() is invoked right after the data is loaded from the database (and after hook_link(), of course).

if ($comment = db_fetch_object($result)) {
  $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  $links = module_invoke_all('link', 'comment', $comment, 1);
  drupal_alter('link', $links, $node, $comment);

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

hook_comment is invoked from theme_comment_view().

if ($visible) {
  $comment->comment = check_markup($comment->comment, $comment->format, FALSE);

  // Comment API hook
  comment_invoke_comment($comment, 'view');

  $output .= theme('comment', $comment, $node, $links);
}
else {
  $output .= theme('comment_folded', $comment);
}

hook_comment(), which is invoked with a call to comment_invoke_comment(), is not called for comments that are not visible.

The implementation of hook_link_alter() you created should at least check which comments it is altering, if it is supposed to alter only specific comments. At least, it should check the last parameter ($comment) is not NULL; if that happens, the hook is invoked to alter node links, which is not what you want as you need to alter comment links.

0

If they are rendered like Drupal comments why don't you use hook_comment()?

Here's a kick-start:

function mymodule_comment(&$a1, $op) {
  if ($op === 'view') {
    // $a1 is the comment object
  }
}
2
  • That's gooding thinking! I'll try it on Monday.
    – Codeblind
    Commented Mar 12, 2011 at 2:03
  • good luck hope it works! :)
    – Alex Weber
    Commented Mar 12, 2011 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.