hook_link_alter doesn't work for comment links

moonray - January 24, 2007 - 23:13
Project:Drupal
Version:7.x-dev
Component:comment.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:won't fix
Description

I've been trying to figure out why I can't alter comment's links (delete, edit, reply), and it turns out that hook_link_alter isn't implemented for comment links, even though they're built using hook_links!

Seems a little inconsistent to me.

Now, I tried patching comment.module to implement it, but then I realized that the comment object is missing a type, which makes it impossible to distinguish it from a $node inside xxx_link_alter().

So, it looks like comment.module needs some tweaking, or hook_link_alter needs some rethinking.
My first impulse is to add a type property to comment so it can be passed to hook_link_alter, and you can distinguish it from a $node.

Your thoughts are welcome on this.

#1

moonray - January 24, 2007 - 23:27
Status:active» needs work

Here's a quick patch I wrote up to get things rolling.

AttachmentSizeStatusTest resultOperations
comment_hook_link_alter.patch8.78 KBIgnoredNoneNone

#2

RobRoy - January 24, 2007 - 23:37

If you were going to go that route, you should just define comment->type = 'comment' in theme_comment_thread_expanded instead of in every db_query. ;)

But, we definitely need a proper solution for 6.x-dev. Why do we need a type in there on the $node object? If type is not specified currently, only hook_link_alter implementations that explicitly check that will not match comments since this isn't currently working for comments themselves. We should probably add a $type param to hook_link_alter itself to specify 'comment', 'node', etc. kinda like $op for hook_taxonomy. But we'll need to pass the $node in for comment links as well...

#3

Gman - April 2, 2007 - 23:05

I agree that some kind of $type variable needs to be implemented in hook_link_alter, just as it is for hook_link. The ability to remove certain links from comments (maybe on a per node-type basis) is really needed. Many modules add those links to all node-types, but a site may only want them on blog and forum comments, without affecting the node links. Adding the $type in the hook_link_alter function will allow such implementations.

#4

Stefan Nagtegaal - August 8, 2007 - 14:15
Status:needs work» postponed (maintainer needs more info)

Is this still an issue in head (drupal 6)? I thought Gurpartap made a patch for this, some while back and got committed.

#5

eaton - August 25, 2007 - 15:43
Version:6.x-dev» 7.x-dev
Status:postponed (maintainer needs more info)» postponed

There's no way to fix this without breaking existing implementations, unfortunately. If we broaden the coverage of this function existing code will choke because it's only written to handle incoming nodes.

We should, in the future, expand this to cover nodes, comments, and users. Passing in the type of the object as a third parameter to drupal_alter() would probably be the best solution.

#6

catch - July 21, 2008 - 12:14
Status:postponed» active

I just ran into this in 5.x. Marking back to active.

#7

mpg - July 23, 2008 - 06:56

please help...

it would be nice to change the comment module "comment" link based on the page -- blog post, review or comment so that you could have the link say "comment on this blog" if it is a blog or "review this item" for a review page --

Is this currently possible in 5.5?

thanks you for your reply, this one has been stumping me for a while.

#8

catch - July 26, 2008 - 20:51
Status:active» needs work

There's a patch here. Also just closed out this dupicate, which has both a patch and a workaround: #169890: hook_link_alter multiple comment view patch

#9

whisk - August 30, 2008 - 13:56

Patch in issue #169890: hook_link_alter multiple comment view patch worked great for my 5.x installations (thanks, subspaceeddy!). But hook_link_alter still isn't completely consistent. It invokes for nodes and comments, but in both cases node object is passed, and it is impossible to retrieve comment object.

I've rewritten the patch a little, so comment.module passes comment object to hook_link_alter. Usage of hook_link_alter changes as following:

<?php
function hook_link_alter ($object, &$links) {
  if (
$object->cid) {
   
// it is a comment
 
}
  elseif (
$object->nid) {
   
// it's a node (no cid, but nid is present)
 
}
  else {
   
// oops
 
}
}
?>

In my case, it doesn't affect third-party modules, but thorough check is required...
This patch allowed me to write a tiny UI module with 'approve', 'hide', 'approve and reply' comment links.

AttachmentSizeStatusTest resultOperations
comment_link_alter2.patch2.42 KBIgnoredNoneNone

#10

Dave Reid - December 21, 2008 - 17:21

This may have changed significantly now that hook_link_alter now only works on comment links. See #339929: Move node links into $node->content.

#11

greggles - February 3, 2009 - 00:03
Status:needs work» duplicate

Agree with Dave Reid in #10. This is already done for 7.x and since it's a change to the API I think it should probably be "wont fixed" for 6.x or earlier.

#12

catch - May 5, 2009 - 12:21
Status:duplicate» postponed

It's not done in D7, but putting to postponed on #451272: Rename or remove or do *something* with hook_link() so we can close it once that's completely resolved.

#13

Damien Tournoud - July 15, 2009 - 08:12
Status:postponed» won't fix

hook_link_alter() only works for comments now.

 
 

Drupal is a registered trademark of Dries Buytaert.