I really hate the way that book.module generates the "printer-friendly" link without checking whether or not the current page is actually of the "book page" type. Instead it seems that the only check is:

if ($type == 'node' && isset($node->parent)) {

which is certainly not an ideal way of doing things... if a module also defines a "parent" attribute to the node, then it will get the printer friendly links no matter what. I have to hack the book.module every time I upgrade.

That put aside, is there any particular reason that hook_link is invoked for every module, not just the one that defines the node type of the current page?

Comments

marcp’s picture

That put aside, is there any particular reason that hook_link is invoked for every module, not just the one that defines the node type of the current page?

comment.module uses it to stick an "add new comment" link to the bottom of any node that should accept comments. i'm sure there are some other good examples out there, too.
-------
http://www.funnymonkey.com
Tools for Teachers

CnnmnSchnpps’s picture

In that case, would it not be more appropriate to file this functionality under hook_nodeapi with $op='link'? And then have hook_links be for links that are exclusively limited to the current module? That way modules don't have to check the content type of the current node on every invocation - if they want to add links to every node, they use hook_nodeapi, if they want to add them to only their own nodes, they use hook_link

Or is there something else that I am missing?

marcp’s picture

Yeah, you have a point. This is from the hook_nodeapi documentation:

If you are writing a node module, do not use this hook to perform actions on your type of node alone. Instead, use the hooks set aside for node modules, such as hook_insert() and hook_form().

So I have to join you in asking ... why isn't nodeapi used by the core in node_view() when adding links to nodes, and shouldn't hook_link just be called in that node's module?

-------
http://www.funnymonkey.com
Tools for Teachers

freetime’s picture

Being a newbie to drupal but a software development teacher, I thought that I can easily create a stub for my own module
based on book.module by just replacing the name "book" by my own name. The book module - by its name and idea - does not give the impression of adding something to any node.

Therefore, I support the argument:

- if node behavior can generally be changed through a hook, then the hooks name should clearly give an indication, e.g. "nodeapi" and
the "hook_link" should only be called for the respective module.

- book.module would better check for node type, while it is not so.

I applied the respective patch to both the book.module and my own module. Thanks for the help :-)