How do I name hooks if I want multiple node types in one module? Hook_node_info clearly allows multiple node types to be declared within 1 module, but how does Drupal use hooks to find these node types, if the hooks are named after the module and not the node type?
I have read in the API documentation here and in the book "Pro Drupal" that modules can have multiple nodes, but I can't find docs? Am I missing the jargon that is the keyword, or are there no docs?
Example context: A "dictionary" module that declares an "index" object that has two fields, "source" and "target", and an "item" object that can be either source or target in an index. (This is like a translation dictionary-- items are words, and the index lists translations between words.)
A basic module concept is that hooks are named for their module (http://api.drupal.org/api/group/hooks/5): "http://api.drupal.org/api/group/hooks/5"
But there are many hooks that react to node types, instead of the module. Consider hook_form (http://api.drupal.org/api/function/hook_form/5): "This hook needs to return the node title, the body text area, and fields specific to the node type."
So for hook_form: do I have one "dictionary_form" hook using case statements to alter response to type in $node param?
Or, do I have index_form, item_form etc for each entry. I'm not interested in "Yes, have those but call from dictionary_form." My question is, "What hook name will Drupal call when a form is needed for an object declared in a node with multiple node declarations."
Or working backwards, a user requests an edit form for an "index" item, will Drupal call "index_node_form" or "dictionary_node_form" and why? Is there a registry declaring what node types go with what module, so that Drupal always knows to call the right hook?
I appreciate any help a whole lot!
Comments
...
Yes, that's one possibility. You can examine
$node->typein your hooks and do different things to different nodes depending on their type. That's why hooks can be shared among node-types.That's, BTW, what Drupal itself does. When you create various node-types, via
q=admin/content/types/add, Drupal doesn't create hook functions for each type: it shares a handful of hook functions among the node-types.But there is another, different method to implement several node types. This method does not involve sharing the hooks among the types. If you check the documentation for hook_node_info you'll find out that you must return a 'module' member. But this 'module' is actually a misnomer: this member was named 'base' in previous Drupal versions. It specifies the prefix to attach to the hook names --for this node-type. It doesn't really specifies the module name. Example:
This method has the disadvantage that you have to hard-code all these functions. So you have to know in advance all the node-types you're going to provide. On the other hand, if your module is a 'factory' for node types --that is, if the node-types you provide are determined programmatically-- you can't use this method.
It uses the 'module' member (returned by hook_node_info()) as a prefix and adds to it the name of the hook.
(Node-types created via
q=admin/content/types/addhave the 'node_content' psuedo module associated with them so that they don't clash with functions defined by the node module itself.)Thanks
Just realized I never thanked you. Then again, it took close to three months for my tiny brain to process all of that.
Thanks
Excellent in-depth explanation. I think this deserves to be in the module developer's guide.